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

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


当前回答

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

其他回答

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

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

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 = {}

用法:

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

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

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

>>> 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()

Time.time()就可以了。

import time

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

elapsed = end - start

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

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

为了最好地测量运行时间(自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的更新

时间。时钟现在没了。