Skip to Main Content
高效能PYTHON程式設計
book

高效能PYTHON程式設計

by Micha Gorelick, Ian Ozsvald
August 2015
Intermediate to advanced content levelIntermediate to advanced
384 pages
7h 42m
Chinese
GoTop Information, Inc.
Content preview from 高效能PYTHON程式設計
透過效能分析找出瓶頸
|
37
叫其他函式上(唯一比較重要的是
abs
)。可以看到,只有一點點時間花在準備工作上,
大多數執行時間都耗費在計算上。
使用
runsnake
時,你可以點擊函式,並且深入複雜的嵌套呼叫(nested call)。當團隊在
討論某段程式碼執行緩慢的原因時,這項工具是非常寶貴的。
2-5 RunSnakeRun 視覺化 cProfile 的效能分析檔案
使用 line_profiler 進行一行一行的量測
Ian 的觀點來看,Robert Kern
line_profiler
是辨識 Python 程式碼裡頭計算密集型
問題之成因的最強大工具,它在一行一行的基礎上,針對個別的函式進行效能分析,因
此,你應該從
cProfile
開始,透過高階觀點,判斷出要使用
line_profiler
深入分析哪
些函式。
在修改程式碼時,值得花時間列印及註釋這項工具的輸出版本,所以你會有能夠迅速參
照的變更紀錄(成功或不成功)。在進行一行一行的程式碼變更時,不要單單倚賴你的
記憶力。
38
|
第二章
要安裝
line_profiler
,就執行
pip
install
line_profiler
命令。
裝飾器(
@profile
)被用來標記選取的函式,
kernprof.py
指令稿被用來執行你的程式
碼,而且,CPU ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

流畅的Python

流畅的Python

Luciano Ramalho
手把手教会你linux

手把手教会你linux

桑德.范.乌格特

Publisher Resources

ISBN: 9789863477105