python计算时间差
前言
之前写代码都是看打印的初始和结束时间然后自己算间隔时间,感觉总是不方便,这不符合python的优雅简洁,于是去寻找简便之道.
方法
time模块计算时间差
import time
s_time = time.time()
for i in range(1000):
print(i)
c_time = time.time()
ss_time = c_time - s_time
print('%.3f秒'%ss_time)
print('执行结束!!!')
datetime时间模块计算时间差
from datetime import datetime
a = datetime.now()
for x in range(10000):
print(x)
b = datetime.now()
print(a)
print(b)
print("运行时间:"+str(b-a)+" 秒")
from datetime import datetime
a = datetime.now()
for x in range(100000):
print(x)
b = datetime.now()
print(a)
print(b)
print("运行时间:"+str((b-a).seconds)+" 秒")
from datetime import datetime
a = datetime(2020, 10, 28, 14, 41, 9, 386637)
b = datetime(2020, 11, 29, 14, 41, 9, 386637)
print(a)
print(b)
print("运行时间:"+str((b-a).days)+" 天")
本文来自博客园,作者:Harry_666,转载请注明原文链接:https://www.cnblogs.com/harry66/p/13895044.html