方法一
import time
start = time.clock() #Python3.8不支持clock了,使用timer.perf_counter()
#需要计算的代码块
end = time.clock()
print('程序运行时间为: %s Seconds'%(end-start))
方法二
import time
start=time.time()
#需要计算的代码块
end=time.time()
print('程序运行时间为: %s Seconds'%(end-start))
方法三
import datetime
start = datetime.datetime.now()
#需要计算的代码块
end = datetime.datetime.now()
print('程序运行时间为: %s Seconds'%(end-start))
本文介绍了三种在Python中测量代码运行时间的方法:使用time模块的clock()和time()函数,以及datetime模块的datetime.now()。这些方法可以帮助开发者优化代码性能,了解代码执行效率。
1231

被折叠的 条评论
为什么被折叠?



