方法一:
import datetime import time starttime = datetime.datetime.now() print(starttime.strftime("%Y-%m-%d %H:%M:%S")) time.sleep(2) endtime = datetime.datetime.now() print(endtime.strftime("%Y-%m-%d %H:%M:%S")) costtime = round((endtime - starttime).total_seconds(),2) print ('运行时间为:{}秒'.format(costtime))
方法二:
# 导入time库 import time # 代码开始运行 start = time.perf_counter() print(start) time.sleep(2) # 代码结束运行 end = time.perf_counter() print(end) # 计算运行时间,单位为秒 print('运行时间为:{}秒'.format(end-start))
标签:-%,endtime,Python,datetime,start,time,print,执行,统计 From: https://www.cnblogs.com/yeyuzhuanjia/p/16902700.html