2024-04-27 03:32:29 发布
网友
如何计算python中的程序运行时间?
您可能需要查看timeit模块:
timeit
http://docs.python.org/library/timeit.html
或者profile模块:
profile
http://docs.python.org/library/profile.html
这里还有一些很好的教程:
http://www.doughellmann.com/PyMOTW/profile/index.html
http://www.doughellmann.com/PyMOTW/timeit/index.html
而且time模块也可能会派上用场,尽管我更喜欢后面两个关于基准测试和分析代码性能的建议:
time
http://docs.python.org/library/time.html
快速选择
import timeit start = timeit.default_timer() #Your statements here stop = timeit.default_timer() print('Time: ', stop - start)
我不知道这是否是一个更快的选择,但我有另一个解决方案-
from datetime import datetime start=datetime.now() #Statements print datetime.now()-start
您可能需要查看
timeit
模块:http://docs.python.org/library/timeit.html
或者
profile
模块:http://docs.python.org/library/profile.html
这里还有一些很好的教程:
http://www.doughellmann.com/PyMOTW/profile/index.html
http://www.doughellmann.com/PyMOTW/timeit/index.html
而且
time
模块也可能会派上用场,尽管我更喜欢后面两个关于基准测试和分析代码性能的建议:http://docs.python.org/library/time.html
快速选择
我不知道这是否是一个更快的选择,但我有另一个解决方案-
相关问题 更多 >
编程相关推荐