paip.效能跟蹤profile原理與架構與本質 python掃帶java php

來源:互聯網
上載者:User
paip.效能跟蹤profile原理與架構與本質-- python掃帶java php

##背景
弄個個IME音標轉換atiEnPH工具,老是python效能不的上K,7k記錄瀏覽過k要30分鐘了.

##目標
分析一個程式的效能,最終都歸結為回答4個基本的問題:

程式運行速度有多快?
運行速度瓶頸在哪兒?
程式使用了多少記憶體?
記憶體泄露發生在哪裡?
誰引用著泄漏的對象?


作者 老哇的爪子 Attilax 艾龍, EMAIL:1466519819@qq.com
轉載請註明來源: http://blog.csdn.net/attilax

#工具cProfile,objgraph
誰引用著泄漏的對象? 這個可以使用對象引用圖來看見..
該命令的輸出應該是一副PNG映像,儲存在/tmp/backrefs.png,它看起來是像這樣:

back refrences

最下面有紅字的盒子是我們感興趣的對象。我們可以看到,它被符號x引用了一次,被列表y引用了三次。如果是x引起了一個記憶體流失,我們可以使用這個方法,通過跟蹤它的所有引用,來檢查為什麼它沒有自動的被釋放。

回顧一下,objgraph 使我們可以:

顯示佔據python程式記憶體的頭N個對象
顯示一段時間以後哪些對象被刪除活增加了
在我們的指令碼中顯示某個給定對象的所有引用

#測試代碼
import cProfile

#直接把分析結果列印到控制台
cProfile.run("mainx()")
#把分析結果儲存到檔案中,不過內容可讀性差...需要調用pstats模組分析結果
#cProfile.run("foo()", "result")
#還可以直接使用命令列進行操作
#>python -m cProfile myscript.py -o result


def convert2atiEnPn(phntc):#17.3s 49tse
""" æk@sentjueit >> e@k@sen@tju@ei@t
"""
r=""

#print ("o412")
#print(map)
#100test logx abt 3s
#logx(" phntc:---"+phntc)
li= SybalbeList(phntc) #7.6s
#logx(" syblist:---")
#print_li(li)
for sbl in li:
#all saveval invoke time safeVal 5.6s 446tse
#todox gaicheng jude modul ..only 0.1s
#yuln ...python d excepti jon fei resource l a .
myvowel=safeVal (map, sbl.vowel, "")
mycnst=safeVal (map, sbl.csnt , "")
r=r+"@"+mycnst+"-"+myvowel
# phntc=trim(phntc)

return r

#第一的測試
157277 function calls in 20.648 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 20.648 20.648 :1()
49 0.006 0.000 17.359 0.354 :15(convert2atiEnPn)
49 0.003 0.000 1.105 0.023 :2(updatexo4)
1 0.003 0.003 20.648 20.648 :37(mainx)

#第二次:

118375 function calls in 9.266 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 9.265 9.265 :1()
49 0.002 0.000 6.395 0.131 :15(convert2atiEnPn)
49 0.003 0.000 1.155 0.024 :2(updatexo4)
1 0.004 0.004 9.265 9.265 :39(mainx)

#分析結果:
能看見,python的異常很消耗效能. 使用 if key in map 代替異常,,,效能消耗從5s直到個0.1s
日誌也比較消耗效能

#java跟php的profile
java jprofile,.. php xdebug..


參考
關於Python Profilers效能分析器 - btchenguang - 部落格園.htm
Python效能分析指南 - 技術翻譯 - 開源中國社區.htm
Python--字典-fussfuss1-ChinaUnix部落格.htm
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.