Python性能分析工具Profile

系统 1133 0

Python性能分析工具Profile

 

 

            
              代码优化的前提是需要了解性能瓶颈在什么地方,程序运行的主要时间是消耗在哪里,对于比较复杂的代码可以借助一些工具来定位,python 内置了丰富的性能分析工具,如 profile,cProfile 与 hotshot 等。其中 Profiler 是 python 自带的一组程序,能够描述程序运行时候的性能,并提供各种统计帮助用户定位程序的性能瓶颈。Python 标准模块提供三种 profilers:cProfile,profile 以及 hotshot。
profile 的使用非常简单,只需要在使用之前进行 
            
            
              import
            
             即可,也可以在命令行中使用。
          

 

使用Profile

测试示例:

            
              import
            
            
               profile

            
            
              def
            
            
               a():
    sum 
            
            =
            
               0
    
            
            
              for
            
             i 
            
              in
            
             range(1, 10001
            
              ):
        sum 
            
            +=
            
               i
    
            
            
              return
            
            
               sum


            
            
              def
            
            
               b():
    sum 
            
            =
            
               0
    
            
            
              for
            
             i 
            
              in
            
             range(1, 100
            
              ):
        sum 
            
            +=
            
               a()
    
            
            
              return
            
            
               sum

            
            
              if
            
            
              __name__
            
             == 
            
              "
            
            
              __main__
            
            
              "
            
            
              :
   profile.run(
            
            
              "
            
            
              b()
            
            
              "
            
            )
          

输出结果:

            
104 function calls in 0.094 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.094 0.094 :0(exec) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.000 0.000 0.094 0.094 :1( ) 1 0.000 0.000 0.094 0.094 profile:0(b()) 0 0.000 0.000 profile:0(profiler) 99 0.094 0.001 0.094 0.001 test.py:15(a) 1 0.000 0.000 0.094 0.094 test.py:21(b)

            其中输出每列的具体解释如下:

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

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

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

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

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

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

如果需要将输出以日志的形式保存,只需要在调用的时候加入另外一个参数。如 profile.run(“profileTest()”,”testprof”)。

          

 

 

命令行

如果我们不想在程序中调用profile库使用,可以在命令行使用命令。

            
              import
            
            
               os


            
            
              def
            
            
               a():
    sum 
            
            =
            
               0
    
            
            
              for
            
             i 
            
              in
            
             range(1, 10001
            
              ):
        sum 
            
            +=
            
               i
    
            
            
              return
            
            
               sum


            
            
              def
            
            
               b():
    sum 
            
            =
            
               0
    
            
            
              for
            
             i 
            
              in
            
             range(1, 100
            
              ):
        sum 
            
            +=
            
               a()
    
            
            
              return
            
            
               sum


            
            
              print
            
             b()
          

 

运行命令查看性能分析结果

python -m cProfile test .py

 

将性能分析结果保存到result文件

python -m cProfile -o result test .py

 

使用pstats来格式化显示结果

python - c "import pstats; p=pstats.Stats('reslut); p.print_stats()"

 

python - c "import pstats; p=pstats.Stats('result'); p.sort_stats('time').print_stats()

            sort_stats支持一下参数:

calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time

          

 

 

测试示例:在代码中直接使用profile与stats

            
              import
            
            
               os

            
            
              def
            
            
               a():
sum 
            
            =
            
               0

            
            
              for
            
             i 
            
              in
            
             range(1, 10001
            
              ):
sum 
            
            +=
            
               i

            
            
              return
            
            
               sum

            
            
              def
            
            
               b():
sum 
            
            =
            
               0

            
            
              for
            
             i 
            
              in
            
             range(1, 100
            
              ):
sum 
            
            +=
            
               a()

            
            
              return
            
            
               sum

            
            
              print
            
            
               b()

            
            
              import
            
            
               cProfile

            
            
              #
            
            
              cProfile.run("b()")
            
            
cProfile.run(
            
              "
            
            
              b()
            
            
              "
            
            , 
            
              "
            
            
              result
            
            
              "
            
            
              )

            
            
              import
            
            
               pstats
pstats.Stats(
            
            
              '
            
            
              result
            
            
              '
            
            ).sort_stats(-1).print_stats()
          

 

refence

https://blog.csdn.net/xiemanR/article/details/69398057

https://www.cnblogs.com/wangjian8888/p/6095772.html

https://blog.csdn.net/kongxx/article/details/52216850

http://ju.outofmemory.cn/entry/46805


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论