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

高效能PYTHON程式設計

by Micha Gorelick, Ian Ozsvald
August 2015
Intermediate to advanced
384 pages
7h 42m
Chinese
GoTop Information, Inc.
Content preview from 高效能PYTHON程式設計
18
|
第二章
有時候,懶惰不是什麼壞事,透過先進行效能分析,你能夠迅速地識別出需要被解決的
瓶頸,於是,你可以解決恰恰足夠的問題,就能夠達成需要的效能。如果避開效能分
析,直接投入最佳化,那麼,長期下來,你很可能會做更多的工作。因此,遵循效能分
析的結果來做事,才是明智的選擇。
有效率地進行效能分析
效能分析的首要目標就是測試代表性系統,識別出執行緩慢(或者使用太多 RAM,或
造成太多磁碟 I/O 與網路 I/O)的部分。效能分析通常會增加系統負擔(拖慢 10 倍到
100 倍是很平常的),而且你還想要讓你的程式碼盡可能像在真實世界裡那樣被使用。擷
取出測試案例並且隔離你需要測試的系統零件,最好的情況是,它已經被撰寫在自己的
一群模組裡。
在本章中,首先被引進的基本技術包括 IPython
%timeit
魔法,
time.time()
,以及計
時裝飾器(timing decorator)。你可以利用這些技術瞭解陳述式與函式的行為。
接著,我們將探討
cProfile
(參見第 31 頁的〈使用 cProfile 模組〉),說明如何使用這
項內建工具,來瞭解你的程式碼裡花最長時間運行的函式有哪些,這會讓你掌握問題的
高階觀點,好讓你將注意力集中在關鍵的函式上。
接下來,我們會檢視
line_profiler
(參見第 37 頁的〈使用 line_profiler 進行一行一行
的量測〉),這會在一行一行的基礎上,針對你所選擇的函式進行效能分析,結果將包括
每一行被呼叫的次數,以及花在每一行的時間百分比。這正是理解哪個部分執行得最緩 ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kafka权威指南

Kafka权威指南

Neha Narkhede, Gwen Shapira, Todd Palino
手把手教会你linux

手把手教会你linux

桑德.范.乌格特
流畅的Python

流畅的Python

Luciano Ramalho
精通機器學習

精通機器學習

Aurélien Géron

Publisher Resources

ISBN: 9789863477105