为什么相同的python代码具有不同的时钟时间? [英] why same python code has different clock time?

查看:80
本文介绍了为什么相同的python代码具有不同的时钟时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下python代码对服务器进行基准测试:

I'm benchmarking my server using following python code:

import time
initial_clock = time.clock()
res = 0
for i in range(1, 10000000):
    res += i * i
print (time.clock() - initial_clock)

当我多次运行它时,我得到的执行时间从 2.163377 秒变为 2.970836 秒.我知道同一代码可能由于CPU负载的变化而具有不同的执行时间,但是正如 time.clock 文档中所述,它仅考虑当前进程时钟,因此使用 time时它应该具有相同的执行时间..clock(),即使使用 time.time()也不同.不应该吗?

When I run it multiple times I get different execution times from 2.163377 seconds to 2.970836 seconds. I know same code may have different execution time due to variation in CPU load but as is said in time.clock documentation it only considers current process clocks, so it should have same execution time using time.clock() even if it is different using time.time(). shouldn't be?

反正我还能得到一段Python代码的准确 clock 计数吗?

Also is there anyway I can get exact clock count for a piece of python code?

我使用 time.process_time()并获得相同的结果.

I use time.process_time() and get same result.

推荐答案

时序代码的第一条规则:使用 timeit 模块.这样可以自动为您的操作系统选择最佳的计时器,最大程度地减少其他影响,并进行多次计时以使您最准确地估计所用的时间.

First rule of timing code: use the timeit module. This takes care of picking the best timer for your OS automatically, minimises other influences, and takes multiple timing runs to give you the most accurate estimate of time taken.

接下来, time.clock() 不是特定于进程的.是的,这是CPU上的时钟时间,但不是当前进程使用CPU所花费的时间.计划在CPU上运行的其他进程将增加所花费的时间.您想使用 time.process_time() .

Next, time.clock() is not process specific. It's the clock time on the CPU, yes, but not the amount of time the current process has spent using the CPU. Other processes scheduled to run on the CPU will add fluctuation to the time taken. You'd want to use time.process_time() instead.

但是,所有代码都可能受到缓存和特定于Python的定期处理,例如垃圾收集器正在运行.您永远都不会期望得到可重复的 time.clock() time.process_time()值,因为该时钟无法说明Python中的此类后台工作处理本身.您可以在计时代码时禁用垃圾收集器进程( timeit 为您执行此操作),但这对内部缓存(特定于实现且大多数未记录)的内部缓存无济于事.

However, all code is subject to potential caching and to Python-specific periodic processes such as the garbage collector running. You can't expect to ever get repeatable time.clock() or time.process_time() values, because that clock can't account for such background work in the Python process itself. You can disable the garbage collector processes (timeit does this for you) while timing code but this won't help with internal caches (which are implementation specific and most are not documented).

timeit 使用 时间.perf_counter() 包括花费在I/O和其他进程上的时间,这些时间可能会被测试代码所触发.您可以告诉它使用 time.process_time()代替使用 timeit 作为命令行脚本或通过传递 -p 开关调用API函数时 timer = time.process_time .

timeit uses time.perf_counter() to include time spent on I/O and other processes that might be kicked off by the code under test. You can tell it to use time.process_time() instead by using the -p switch when using timeit as a command line script, or passing timer=time.process_time when calling API functions.

这篇关于为什么相同的python代码具有不同的时钟时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆