gpt4 book ai didi

python - 计算 python 脚本执行时间的最简单方法?

转载 作者:太空狗 更新时间:2023-10-30 01:47:04 24 4
gpt4 key购买 nike

计算 Python 脚本执行时间的最简单方法是什么?

最佳答案

timeit模块专为此目的而设计。

愚蠢的例子如下

def test():
"""Stupid test function"""
L = []
for i in range(100):
L.append(i)

if __name__ == '__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()

Note that timeit can also be used from the command line (python -m timeit -s 'import module' 'module.test()') and that you can run the statement several times to get a more accurate measurement. Something I think time command doesn't support directly. -- jcollado

关于python - 计算 python 脚本执行时间的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8631743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com