python计算时间间隔(精确到微妙)

2023-01-31 05:01:23 间隔 精确 微妙

使用python中的datetime

import datetime
oldtime=datetime.datetime.now()
print oldtime;
x=1
while x<10000000:
    x=x+1
newtime=datetime.datetime.now()
print newtime;
print u'相差:%s'%(newtime-oldtime)
print u'相差:%s微秒'%(newtime-oldtime).microseconds
print u'相差:%s秒'%(newtime-oldtime).seconds

结果如下:

相关文章