计算python脚本执行时间的最简单方法? [英] Easiest way to calculate execution time of a python script?

查看:260
本文介绍了计算python脚本执行时间的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What's the easiest way to calculate the execution time of a Python script?

推荐答案

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

timeit module is designed specifically for this purpose.

如下愚蠢的例子

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()

请注意,timeit也可以从命令行使用(python -m timeit -s'导入模块''module.test()'),然后您可以运行 多次声明以获得更准确的测量结果.某物 我认为时间命令不直接支持. -jcollado

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脚本执行时间的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆