日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python性能分析工具Profile

系統 1703 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條評論
主站蜘蛛池模板: 景德镇市| 赫章县| 汨罗市| 彝良县| 凌海市| 永康市| 峨眉山市| 黄骅市| 神池县| 定襄县| 天水市| 太白县| 洪江市| 虹口区| 冕宁县| 牡丹江市| 七台河市| 北碚区| 霍山县| 吴忠市| 樟树市| 行唐县| 湖北省| 桃江县| 康乐县| 鄂州市| 莫力| 沁源县| 平乐县| 陆川县| 增城市| 台南县| 翁牛特旗| 隆回县| 泸水县| 邹平县| 定安县| 高邮市| 白水县| 涡阳县| 葵青区|