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.clock()
会更好。 但是,如果您正在为某些硬件计时(例如,您在GPU中放入的某个算法),那么time.clock()
将摆脱这一次,而time.time()
是唯一剩下的解决方案。注意:无论使用什么方法,计时将取决于您无法控制的因素(进程何时切换,切换频率,…),这对于
time.time()
来说更糟,但对于time.clock()
来说也存在,因此您不应只运行一个计时测试,而应始终运行一系列测试并查看时间的平均值/方差。从3.3开始,time.clock() is deprecated,建议改用time.process_time()或time.perf_counter()。
之前在2.7中,根据time module docs:
此外,还有用于基准代码段的timeit模块。
Others回答了re:
time.time()
对time.clock()
。但是,如果您正为基准测试/分析目的对代码块的执行进行计时,则应该查看^{} module 。
相关问题 更多 >
编程相关推荐