python 求程序运行时间的方法

方法一:时间为s

start_time = time.time()
# 中间为程序体
end_time = time.time()

print("start_time: ",start_time)
print("Time: %d sec" % int(end_time - start_time))

# 输出结果如下:
start_time:  1649902694.371341
Time: 2 sec

方法一:时间为ms

start_timestamp = datetime.now()
# 中间为程序体
end_timestamp = datetime.now()
time_diff = (end_timestamp - start_timestamp)
self.execution_time = time_diff.total_seconds() * 1000

print("start_timestamp: ",start_timestamp)
print(f'execution_time : {execution_time} ms')

# 输出结果为:
start_timestamp:  2022-04-14 10:18:14.371346
execution_time : 2049.537 ms

 

posted @ 2022-04-14 10:26  saaas  阅读(545)  评论(0编辑  收藏  举报