2017-06-01 40 views
3

有一些帖子討論使用cProfile進行python分析,以及由於以下示例代碼中的輸出文件重新編譯而導致的分析輸出的挑戰不是純文本文件。在pandas數據框中存儲cProfile輸出

(顯示的代碼只是https://docs.python.org/2/library/profile.html的樣本,不能直接重現)。

​​

有一個討論,在這裏:Profile a python script using cProfile into an external file

而且在docs.python.org有關於如何分析利用pstats.Stats

(仍然只有一個樣本的輸出更多的細節,而不是可重現的):

import pstats 
p = pstats.Stats('restats') 
p.strip_dirs().sort_stats(-1).print_stats() 

我可能會在這裏錯過一些非常重要的細節,但我窩它確實喜歡將輸出存儲在熊貓數據框中,並從中進行進一步分析。

我認爲這將是非常簡單的,因爲從運行cProfile.run()在IPython的輸出看上去還算整潔:

In[]: 
cProfile.run('re.compile("foo|bar")' 

Out[]: 

enter image description here

如何進入一個大熊貓這個數據有什麼建議幀格式相同?

回答

3

看起來https://github.com/ssanderson/pstats-view可能會做你想要什麼(儘管有相關的可視化數據並使其互動不必要的依賴):

>>> from pstatsviewer import StatsViewer 
>>> sv = StatsViewer("/path/to/profile.stats") 
>>> sv.timings.columns 
Index(['lineno', 'ccalls', 'ncalls', 'tottime', 'cumtime'], dtype='object')