Skip to main content
 首页 » 编程设计

python之Mac 和 Windows 中的 Pythons time.clock() 有什么区别

2025年05月04日13yjmyzz

我正在使用 Python 的时间来衡量 Selenium 进程的时间范围。我的脚本是这样的...

start_time = time.clock() 
... 
#ending with 
final_time = '{0:.2f}'.format(time.clock()-start_time) 

在 Windows 操作系统上运行时,我会得到类似 55.22 的信息,但如果在 Mac 上运行,它会返回类似 .14 的信息,即使时间差不多.

知道 Mac 上发生了什么不同吗?实际上,我也打算在 Ubuntu 上尝试一下,看看有什么不同。

请您参考如下方法:

根据 the documentation , time.clock 在 Unix(包括 Mac OS X)和 Windows 之间是不同的:

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

如果你想要跨平台的一致性,考虑time.time .

处理器时间 和挂钟时间 之间的区别在 article by Doug Hellmann 中有解释。 - 基本上,处理器时钟只有在您的进程正常工作时才会推进。