与时间模块在python是可能测量经过的时间?如果是,我该怎么做?

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


当前回答

在编程中,主要有两种测量时间的方法,结果不同:

>>> print(time.process_time()); time.sleep(10); print(time.process_time())
0.11751394000000001
0.11764988400000001  # took  0 seconds and a bit
>>> print(time.perf_counter()); time.sleep(10); print(time.perf_counter())
3972.465770326
3982.468109075       # took 10 seconds and a bit

Processor Time: This is how long this specific process spends actively being executed on the CPU. Sleep, waiting for a web request, or time when only other processes are executed will not contribute to this. Use time.process_time() Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time. Use time.perf_counter() time.time() also measures wall-clock time but can be reset, so you could go back in time time.monotonic() cannot be reset (monotonic = only goes forward) but has lower precision than time.perf_counter()

其他回答

瓦迪姆·申德的反应很棒。你也可以使用一个简单的装饰器,如下所示:

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!")

您需要导入时间,然后使用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 

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

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

将打印出来,持续2秒:

'00:00:02'

在7分1秒内:

'00:07:01'

注意,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文档

Time.time()就可以了。

import time

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

elapsed = end - start

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

为了最好地测量运行时间(自Python 3.3起),请使用time.perf_counter()。

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

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

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

在许多实现中,它们实际上可能是相同的东西。

在3.3之前,您只能使用time.clock()。

在Unix上,返回以秒表示的浮点数形式的当前处理器时间。精度,实际上是“处理器时间”的定义,取决于同名C函数的定义。 在Windows上,该函数根据Win32函数QueryPerformanceCounter()返回自第一次调用该函数以来经过的时钟秒数,作为浮点数。分辨率通常优于1微秒。


Python 3.7更新

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

使用这些函数可以进一步消除舍入和浮点错误,特别是在测量非常短的周期时,或者应用程序(或Windows机器)是长时间运行的。

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


Python 3.8的更新

时间。时钟现在没了。