简体   繁体   English

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

[英]Easiest way to calculate execution time of a python script?

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

timeit module is designed specifically for this purpose. timeit模块专门为此目的而设计。

Silly example as follows 愚蠢的例子如下

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. 请注意,timeit也可以在命令行中使用(python -m timeit -s'import module''module.test()'),并且您可以多次运行该语句以获得更准确的度量。 Something I think time command doesn't support directly. 我认为时间命令不直接支持的东西。 -- jcollado - jcollado

Using Linux time command like this : time python file.py 使用像这样的Linux time命令: time python file.py

Or you can take the times at start and at end and calculate the difference. 或者你可以在开始和结束时花时间计算差异。

在linux下:time python script.py

How about using time ? time怎么样?
example: time myPython.py 例如: time myPython.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM