准确测量python函数花费的时间 [英] accurately measure time python function takes

查看:36
本文介绍了准确测量python函数花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测量我的程序的某些部分花费的时间(不是为了调试,而是作为输出中的一个功能).准确性很重要,因为总时间只有几分之一秒.

I need to measure the time certain parts of my program take (not for debugging but as a feature in the output). Accuracy is important because the total time will be a fraction of a second.

当我遇到时间模块"http://docs.python.org/library/timeit.html" rel="noreferrer">timeit,它声称​​避免了许多测量执行时间的常见陷阱.不幸的是,它有一个糟糕的界面,将一个字符串作为输入,然后进行 eval 的输入.

I was going to use the time module when I came across timeit, which claims to avoid a number of common traps for measuring execution times. Unfortunately it has an awful interface, taking a string as input which it then eval's.

那么,我需要使用这个模块来准确测量时间,还是时间就足够了?它所指的陷阱是什么?

So, do I need to use this module to measure time accurately, or will time suffice? And what are the pitfalls it refers to?

谢谢

推荐答案

根据 Python 文档,这与不同操作系统下时间函数的准确性有关:

According to the Python documentation, it has to do with the accuracy of the time function in different operating systems:

默认定时器函数是platform依赖.在 Windows 上,time.clock()具有微秒级粒度,但time.time() 的粒度是 1/60一秒;在 Unix 上,time.clock() 有1/100 秒的粒度和time.time() 更精确.在任一平台,默认计时器函数测量挂钟时间,而不是CPU时间.这意味着其他在同一台计算机上运行的进程可能会干扰时间......在Unix上,你可以使用 time.clock() 来测量 CPU 时间.

The default timer function is platform dependent. On Windows, time.clock() has microsecond granularity but time.time()‘s granularity is 1/60th of a second; on Unix, time.clock() has 1/100th of a second granularity and time.time() is much more precise. On either platform, the default timer functions measure wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing ... On Unix, you can use time.clock() to measure CPU time.

直接从 timeit.py 的代码中拉取:

To pull directly from timeit.py's code:

if sys.platform == "win32":
    # On Windows, the best timer is time.clock()
    default_timer = time.clock
else:
    # On most other platforms the best timer is time.time()
    default_timer = time.time

此外,它还直接为您设置运行时代码.如果你使用 time 你必须自己做.这当然节省您的时间

In addition, it deals directly with setting up the runtime code for you. If you use time you have to do it yourself. This, of course saves you time

时间设置:

def inner(_it, _timer):
    #Your setup code
    %(setup)s
    _t0 = _timer()
    for _i in _it:
        #The code you want to time
        %(stmt)s
    _t1 = _timer()
    return _t1 - _t0

Python 3:

从 Python 3.3 开始,您可以使用 time.perf_counter()(系统范围的计时)或 time.process_time()(进程范围的计时),就像你过去使用time.clock()的方式:

from time import process_time

t = process_time()
#do some stuff
elapsed_time = process_time() - t

新函数 process_time 将不包括睡眠期间经过的时间.

The new function process_time will not include time elapsed during sleep.

从 Python 3.7 开始,您还可以使用 process_time_ns() 类似于 process_time() 但以纳秒为单位返回时间.

Since Python 3.7 you can also use process_time_ns() which is similar to process_time()but returns time in nanoseconds.

这篇关于准确测量python函数花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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