Python 使用时间模块测量经过的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3620943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:59:07  来源:igfitidea点击:

Measuring elapsed time with the Time module

pythontimeelapsed

提问by rectangletangle

With the Time module in python is it possible to measure elapsed time? If so, how do I do that?

使用python中的时间模块是否可以测量经过的时间?如果是这样,我该怎么做?

I need to do this so that if the cursor has been in a widget for a certain duration an event happens.

我需要这样做,以便如果光标在某个小部件中停留了一段时间,就会发生事件。

采纳答案by Vadim Shender

start_time = time.time()
# your code
elapsed_time = time.time() - start_time

You can also write simple decorator to simplify measurement of execution time of various functions:

您还可以编写简单的装饰器来简化对各种函数执行时间的测量:

import time
from functools import wraps

PROF_DATA = {}

def profile(fn):
    @wraps(fn)
    def with_profiling(*args, **kwargs):
        start_time = time.time()

        ret = fn(*args, **kwargs)

        elapsed_time = time.time() - start_time

        if fn.__name__ not in PROF_DATA:
            PROF_DATA[fn.__name__] = [0, []]
        PROF_DATA[fn.__name__][0] += 1
        PROF_DATA[fn.__name__][1].append(elapsed_time)

        return ret

    return with_profiling

def print_prof_data():
    for fname, data in PROF_DATA.items():
        max_time = max(data[1])
        avg_time = sum(data[1]) / len(data[1])
        print "Function %s called %d times. " % (fname, data[0]),
        print 'Execution time max: %.3f, average: %.3f' % (max_time, avg_time)

def clear_prof_data():
    global PROF_DATA
    PROF_DATA = {}

Usage:

用法:

@profile
def your_function(...):
    ...

You can profile more then one function simultaneously. Then to print measurements just call the print_prof_data():

您可以同时分析多个功能。然后打印测量值只需调用print_prof_data():

回答by lalli

time.time()will do the job.

time.time()会做的工作。

import time

start = time.time()
# run your code
end = time.time()

elapsed = end - start

You may want to look at thisquestion, but I don't think it will be necessary.

你可能想看看这个问题,但我认为没有必要。

回答by Nurul Akter Towhid

You need to import time and then use time.time() method to know current time.

您需要导入时间,然后使用 time.time() 方法知道当前时间。

import time

start_time=time.time() #taking current time as starting time

#here your code

elapsed_time=time.time()-start_time #again taking current time - starting time 

回答by Rutger Hofste

For users that want better formatting,

对于想要更好格式的用户,

import time
start_time = time.time()
# your script
elapsed_time = time.time() - start_time
time.strftime("%H:%M:%S", time.gmtime(elapsed_time))

will print out, for 2 seconds:

将打印出来,2 秒:

'00:00:02'

and for 7 minutes one second:

并持续 7 分钟一秒:

'00:07:01'

note that the minimum time unit with gmtime is seconds. If you need microseconds consider the following:

请注意,gmtime 的最小时间单位是秒。如果您需要微秒,请考虑以下事项:

import datetime
start = datetime.datetime.now()
# some code
end = datetime.datetime.now()
elapsed = end - start
print(elapsed)
# or
print(elapsed.seconds,":",elapsed.microseconds) 

strftime documentation

strftime文档

回答by OrangeDog

For the best measure of elapsed time (since Python 3.3), use time.perf_counter().

对于经过时间的最佳度量(自 Python 3.3 起),请使用time.perf_counter().

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

返回性能计数器的值(以秒为单位),即具有最高可用分辨率的时钟以测量短持续时间。它确实包括睡眠期间经过的时间,并且是系统范围的。返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。

For measurements on the order of hours/days, you don't care about sub-second resolution so use time.monotonic()instead.

对于小时/天数量级的测量,您不关心亚秒级分辨率,因此请time.monotonic()改用。

Return the value (in fractional seconds) of a monotonic clock, i.e. a clock that cannot go backwards. The clock is not affected by system clock updates. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

返回单调时钟的值(以秒为单位),即不能倒退的时钟。时钟不受系统时钟更新的影响。返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。

In many implementations, these may actually be the same thing.

在许多实现中,这些实际上可能是同一件事。

Before 3.3, you're stuck with time.clock().

在 3.3 之前,你被困在time.clock().

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.

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.

在 Unix 上,将当前处理器时间返回为以秒表示的浮点数。精度,实际上是“处理器时间”含义的定义,取决于同名 C 函数的精度。

在 Windows 上,此函数根据 Win32 函数 QueryPerformanceCounter() 以浮点数形式返回自第一次调用此函数以来经过的挂钟秒数。分辨率通常优于一微秒。



Update for Python 3.7

Python 3.7 更新

New in Python 3.7 is PEP 564-- Add new time functions with nanosecond resolution.

Python 3.7 中的新功能是PEP 564——添加具有纳秒分辨率的新时间函数。

Use of these can further eliminate rounding and floating-point errors, especially if you're measuring very short periods, or your application (or Windows machine) is long-running.

使用这些可以进一步消除舍入和浮点错误,尤其是当您测量非常短的时间段,或者您的应用程序(或 Windows 机器)长时间运行时。

Resolution starts breaking down on perf_counter()after around 100 days. So for example after a year of uptime, the shortest interval (greater than 0) it can measure will be bigger than when it started.

perf_counter()大约 100 天后,分辨率开始分解。因此,例如在一年的正常运行时间之后,它可以测量的最短间隔(大于 0)将比开始时更大。



Update for Python 3.8

Python 3.8 更新

time.clockis now gone.

time.clock现在不见了。

回答by Tora

For a longer period.

更长的时间。

import time
start_time = time.time()
...
e = int(time.time() - start_time)
print('{:02d}:{:02d}:{:02d}'.format(e // 3600, (e % 3600 // 60), e % 60))

would print

会打印

00:03:15

if more than 24 hours

如果超过 24 小时

25:33:57

That is inspired by Rutger Hofste's answer. Thank you Rutger!

这是受到 Rutger Hofste 回答的启发。谢谢罗格!

回答by Mohammad

Vadim Shender response is great. You can also use a simpler decorator like below:

Vadim Shender 的反应很棒。您还可以使用更简单的装饰器,如下所示:

import datetime
def calc_timing(original_function):                            
    def new_function(*args,**kwargs):                        
        start = datetime.datetime.now()                     
        x = original_function(*args,**kwargs)                
        elapsed = datetime.datetime.now()                      
        print("Elapsed Time = {0}".format(elapsed-start))     
        return x                                             
    return new_function()  

@calc_timing
def a_func(*variables):
    print("do something big!")

回答by T.M.

Another nice way to time things is to use the withpython structure.

另一种计时的好方法是使用withpython 结构。

withstructure is automatically calling __enter__and __exit__methods which is exactly what we need to time things.

with结构会自动调用__enter____exit__方法,这正是我们计时所需的方法。

Let's create a Timerclass.

让我们创建一个Timer类。

from time import time

class Timer():
    def __init__(self, message):
        self.message = message
    def __enter__(self):
        self.start = time()
        return None  # could return anything, to be used like this: with Timer("Message") as value:
    def __exit__(self, type, value, traceback):
        elapsed_time = (time() - self.start) * 1000
        print(self.message.format(elapsed_time))

Then, one can use the Timer class like this:

然后,可以像这样使用 Timer 类:

with Timer("Elapsed time to compute some prime numbers: {}ms"):
    primes = []
    for x in range(2, 500):
        if not any(x % p == 0 for p in primes):
            primes.append(x)
    print("Primes: {}".format(primes))

The result is the following:

结果如下:

Primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]

素数:[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 , 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 19, 19, 19, 19, 29 , 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 3, 3, 3, 3, 3, 3, 39, 39 , 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 49,487, 49

Elapsed time to compute some prime numbers: 5.01704216003418ms

计算一些素数所用的时间:5.01704216003418ms

回答by Steve

Here is an update to Vadim Shender's clever code with tabular output:

这是对 Vadim Shender 的带有表格输出的巧妙代码的更新:

import collections
import time
from functools import wraps

PROF_DATA = collections.defaultdict(list)

def profile(fn):
    @wraps(fn)
    def with_profiling(*args, **kwargs):
        start_time = time.time()
        ret = fn(*args, **kwargs)
        elapsed_time = time.time() - start_time
        PROF_DATA[fn.__name__].append(elapsed_time)
        return ret
    return with_profiling

Metrics = collections.namedtuple("Metrics", "sum_time num_calls min_time max_time avg_time fname")

def print_profile_data():
    results = []
    for fname, elapsed_times in PROF_DATA.items():
        num_calls = len(elapsed_times)
        min_time = min(elapsed_times)
        max_time = max(elapsed_times)
        sum_time = sum(elapsed_times)
        avg_time = sum_time / num_calls
        metrics = Metrics(sum_time, num_calls, min_time, max_time, avg_time, fname)
        results.append(metrics)
    total_time = sum([m.sum_time for m in results])
    print("\t".join(["Percent", "Sum", "Calls", "Min", "Max", "Mean", "Function"]))
    for m in sorted(results, reverse=True):
        print("%.1f\t%.3f\t%d\t%.3f\t%.3f\t%.3f\t%s" % (100 * m.sum_time / total_time, m.sum_time, m.num_calls, m.min_time, m.max_time, m.avg_time, m.fname))
    print("%.3f Total Time" % total_time)