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程式設計
316
|
第十一章
節省 RAM 的訣竅
通常,就把資料放進 RAM 這件事而言,能省則省。你所載入的一切都會耗用你的
RAM。你或許可以只載入一部分資料,例如,使用記憶體對映檔(memory-mapped
file)(
https://docs.python.org/2/library/mmap.html
);或者,你也許能夠使用生成器,僅載
入進行部分計算所需的資料,而不是一次通通都載入。
如果你正在處理數值資料,那麼,你幾乎肯定會想要改用
numpy
陣列—該套件提供許多
快速的演算法,直接操作底層的基本型別物件,相較於使用數字串列,這會節省龐大的
RAM,而且節省的時間可能同樣驚人。
在這本書裡,你應該會注意到我們通常使用
xrange
,而不是
range
,原因很簡單,因為
(在 Python 2.x 中)
xrange
是生成器,而
range
建構整個串列。只是為了迭代正確的次
數就建造 100,000,000 個整數,實在太不經濟。Python 3.x
range
改變成生成器,所以
你不需要再做這個改變。
如果你正在使用Python 2.x 操作字串,並且想要節省RAM,盡量使用
str
代替
unicode
。如果你的整個程式需要大量的 Unicode 物件,或許直接升級到 Python 3.3+
有比較好的結果。如果你正在將大量 Unicode 物件儲存在靜態結構中,那麼,你可能會
想要研究一下剛剛所討論的 DAWG trie 結構。
如果你正在處理大量位元字串,請研究一下
numpy
bitarray
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