无参数函数
先解释一下时间戳,所谓时间戳,即自1970年1月1日00:00:00所经历的秒数,然后就可以理解下面的函数了。下面代码默认
from time import *
来源gaodai#ma#com搞@代~码$网 | implementation | monotonic | adjustable | resolution |
---|---|---|---|---|
‘time’ | GetSystemTimeAsFileTime() | False | True | 0.015625 |
‘thread_time’ | GetThreadTimes() | True | False | 1e-07 |
‘process_time’ | GetProcessTimes() | True | False | 1e-07 |
‘monotonic’ | GetTickCount64() | True | False | 0.015625 |
‘perf_counter’ | QueryPerformanceCounter() | True | False | 1e-07 |
上面五组函数中,只有time.time()的值具有绝对的意义,其他值都只具有相对的意义。
通过get_clock_info函数可以查看这些时钟的特性,其输入输出分别为
implementation | monotonic | adjustable | resolution | |
---|---|---|---|---|
‘time’ | GetSystemTimeAsFileTime() | False | True | 0.015625 |
‘thread_time’ | GetThreadTimes() | True | False | 1e-07 |
‘process_time’ | GetProcessTimes() | True | False | 1e-07 |
‘monotonic’ | GetTickCount64() | True | False | 0.015625 |
‘perf_counter’ | QueryPerformanceCounter() | True | False | 1e-07 |
其中,
- 如果时钟可以自动更改或由系统管理员手动更改,则adjustable为True,否则为False。
- implementation表示用于获取时钟值的基础C函数的名称。
- 如果时钟不能倒退,则monotonic为 True,否则为 False 。
- resolution表示以秒为单位的时钟分辨率。
接下来可以测试一下这些时钟的特性。
>>> def test(n): ... aTime = time.time() ... aTh = time.thread_time() ... aPr = time.process_time() ... aMo = time.monotonic() ... aPe = time.perf_counter() ... for i in range(int(n)): j = i**2 ... bTime = time.time() ... bTh = time.thread_time() ... bPr = time.process_time() ... bMo = time.monotonic() ... bPe = time.perf_counter() ... aStr = f'aTime={aTime},aTh={aTh},aPr={aPr},aMo={aMo},aPe={aPe}\n' ... bStr = f'bTime={bTime},bTh={bTh},bPr={bPr},bMo={bMo},bPe={bPe}' ... print(aStr+bStr) ... >>> test(1e6) aTime=1634625786.136904,aTh=0.03125,aPr=0.03125,aMo=199082.078,aPe=199085.4751224 bTime=1634625786.340363,bTh=0.234375,bPr=0.234375,bMo=199082.281,bPe=199085.6787309 >>> test(1e6) aTime=1634625789.7817287,aTh=0.234375,aPr=0.234375,aMo=199085.734,aPe=199089.1195357 bTime=1634625789.981198,bTh=0.421875,bPr=0.421875,bMo=199085.921,bPe=199089.3195721 >>> test(1e6) aTime=1634625796.3934195,aTh=0.421875,aPr=0.421875,aMo=199092.343,aPe=199095.731209 bTime=1634625796.5789576,bTh=0.609375,bPr=0.609375,bMo=199092.531,bPe=199095.9172852 >>>