Python脚本性能分析

来自:http://www.cnblogs.com/btchenguang/archive/2012/02/03/2337112.html

def foo():
    sum = 0
    for i in range(10000):
        sum += i
    sumA = bar()
    sumB = bar()
    return sum
     
def bar():
    sum = 0
    for i in range(100000):
        sum += i
    return sum
  
if __name__ == "__main__":
    import cProfile
 
    #直接把分析结果打印到控制台
    cProfile.run("foo()")
    #把分析结果保存到文件中,不过内容可读性差...需要调用pstats模块分析结果
    cProfile.run("foo()", "result")
    #还可以直接使用命令行进行操作
    #>python -m cProfile myscript.py -o result
     
    import pstats
    #创建Stats对象
    p = pstats.Stats("result")
    #这一行的效果和直接运行cProfile.run("foo()")的显示效果是一样的
    p.strip_dirs().sort_stats(-1).print_stats()
    #strip_dirs():从所有模块名中去掉无关的路径信息
    #sort_stats():把打印信息按照标准的module/name/line字符串进行排序
    #print_stats():打印出所有分析信息
 
    #按照函数名排序 
    p.strip_dirs().sort_stats("name").print_stats()
 
    #按照在一个函数中累积的运行时间进行排序
    #print_stats(3):只打印前3行函数的信息,参数还可为小数,表示前百分之几的函数信息
    p.strip_dirs().sort_stats("cumulative").print_stats(3)
 
    #还有一种用法
    p.sort_stats('time', 'cum').print_stats(.5, 'foo')
    #先按time排序,再按cumulative时间排序,然后打倒出前50%中含有函数信息
 
    #如果想知道有哪些函数调用了bar,可使用
    p.print_callers(0.5, "bar")
 
    #同理,查看foo()函数中调用了哪些函数
    p.print_callees("foo")

运行:python -m cProfile t12.py,打印结果:

其中,输出每列的具体解释如下:(http://xianglong.me/article/analysis-python-application-performance-using-cProfile/)

    ncalls:表示函数调用的次数;

    tottime:表示指定函数的总的运行时间,除掉函数中调用子函数的运行时间;

    percall:(第一个percall)等于 tottime/ncalls;

    cumtime:表示该函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间;

    percall:(第二个percall)即函数运行一次的平均时间,等于 cumtime/ncalls;

    filename:lineno(function):每个函数调用的具体信息;

 另外,上面分析的时候,排序方式使用的是函数调用时间(cumulative),除了这个还有一些其他允许的排序方式:calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time

# 增加排序方式

python -m cProfile -s cumulative t2.py #按照cumulative排序
原文地址:https://www.cnblogs.com/dreamer-fish/p/5545325.html