首页 > 解决方案 > Python中的时间粒度

问题描述

在 Windows 10 下 Anaconda 提示下的 Python shell 中,以下程序:

a = datetime.datetime.now().timestamp()
b = a

n = 0
while b == a:
    b = datetime.datetime.now().timestamp()
    n +=1

print(n, a, b-a)

有时打印b-a大约 1 毫秒,有时大约 15.61 毫秒。为什么是这样?

>>> exec(open("stamp.py").read())
602 1562500102.504166 0.015616178512573242
>>> exec(open("stamp.py").read())
640 1562500103.832325 0.015614986419677734
>>> exec(open("stamp.py").read())
788 1562500106.503157 0.0009992122650146484
>>> exec(open("stamp.py").read())
8270 1562500108.457863 0.015616893768310547
>>> exec(open("stamp.py").read())
6245 1562500113.863885 0.015617132186889648
>>> exec(open("stamp.py").read())
9032 1562500116.035434 0.015618085861206055
>>> exec(open("stamp.py").read())
2889 1562500117.547767 0.015616178512573242
>>> exec(open("stamp.py").read())
256 1562500119.647477 0.0010001659393310547

标签: pythontime

解决方案


推荐阅读