有没有简单的方法可以对python脚本进行基准测试? [英] Is there any simple way to benchmark python script?

查看:132
本文介绍了有没有简单的方法可以对python脚本进行基准测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我使用shell命令time.我的目的是测试数据是小数据集,小数据集,大数据集还是非常大的数据集,以及所需的时间和内存使用量.

Usually I use shell command time. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.

有任何适用于Linux的工具或仅适用于python的工具吗?

Any tools for linux or just python to do this?

推荐答案

看看 timeit Python分析器评论在下面的nikicc 中提到" SnakeViz ".它为您提供了性能分析数据的另一种可视化效果.

Have a look at timeit, the python profiler and pycallgraph. Also make sure to have a look at the comment below by nikicc mentioning "SnakeViz". It gives you yet another visualisation of profiling data which can be helpful.

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

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

基本上,您可以将python代码作为字符串参数传递给它,它将在指定的时间内运行并显示执行时间.文档中的重要内容:

Essentially, you can pass it python code as a string parameter, and it will run in the specified amount of times and prints the execution time. The important bits from the docs:

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)

使用给定的语句 setup 创建Timer实例 代码和 timer 函数,并使用以下命令运行其timeit方法 number 次执行.

Create a Timer instance with the given statement, setup code and timer function and run its timeit method with number executions.

...和:

Timer.timeit(number=1000000)

Timer.timeit(number=1000000)

主语句的

时间 number 次执行.这将执行设置 语句一次,然后返回执行main所需的时间 语句多次(以秒为单位,以浮点数表示). 参数是循环的次数,默认为1 百万.主语句,设置语句和计时器功能 将要使用的参数传递给构造函数.

Time number executions of the main statement. This executes the setup statement once, and then returns the time it takes to execute the main statement a number of times, measured in seconds as a float. The argument is the number of times through the loop, defaulting to one million. The main statement, the setup statement and the timer function to be used are passed to the constructor.

注意

默认情况下,timeit在计时期间暂时关闭garbage collection.这种方法的优点是 它使独立计时更具可比性.这个缺点是 GC可能是绩效表现的重要组成部分 被测量的功能.如果是这样,可以重新启用GC作为第一个 setup 字符串中的语句.例如:

By default, timeit temporarily turns off garbage collection during the timing. The advantage of this approach is that it makes independent timings more comparable. This disadvantage is that GC may be an important component of the performance of the function being measured. If so, GC can be re-enabled as the first statement in the setup string. For example:

timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit()

分析

剖析将为您提供很多关于正在发生的事情的更详细的想法.这是官方文档中的即时示例":

Profiling

Profiling will give you a much more detailed idea about what's going on. Here's the "instant example" from the official docs:

import cProfile
import re
cProfile.run('re.compile("foo|bar")')

哪个会给你:

      197 function calls (192 primitive calls) in 0.002 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.001    0.001 <string>:1(<module>)
     1    0.000    0.000    0.001    0.001 re.py:212(compile)
     1    0.000    0.000    0.001    0.001 re.py:268(_compile)
     1    0.000    0.000    0.000    0.000 sre_compile.py:172(_compile_charset)
     1    0.000    0.000    0.000    0.000 sre_compile.py:201(_optimize_charset)
     4    0.000    0.000    0.000    0.000 sre_compile.py:25(_identityfunction)
   3/1    0.000    0.000    0.000    0.000 sre_compile.py:33(_compile)

这两个模块都应该使您了解在哪里寻找瓶颈.

Both of these modules should give you an idea about where to look for bottlenecks.

此外,要掌握profile的输出,请查看这篇文章

Also, to get to grips with the output of profile, have a look at this post

此模块使用graphviz创建如下的调用图:

This module uses graphviz to create callgraphs like the following:

您可以按颜色轻松查看哪些路径消耗最多的时间.您可以使用pycallgraph API或使用打包的脚本来创建它们:

You can easily see which paths used up the most time by colour. You can either create them using the pycallgraph API, or using a packaged script:

pycallgraph graphviz -- ./mypythonscript.py

虽然开销相当可观.因此,对于已经运行很长时间的流程,创建图形可能需要一些时间.

The overhead is quite considerable though. So for already long-running processes, creating the graph can take some time.

这篇关于有没有简单的方法可以对python脚本进行基准测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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