• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

表格梳理解析python内置时间模块看完就懂

python 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论
文章目录[隐藏]

无参数函数

先解释一下时间戳,所谓时间戳,即自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
>>>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:表格梳理解析python内置时间模块看完就懂
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址