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程式設計
304
|
第十一章
我們可以使用
memit
看看行程的增長情形,驗證這個估計。在此案例中,數字非常
接近︰
In [2]: %memit([x for x in xrange(10000000)])
peak memory: 330.64 MiB, increment: 310.62 MiB
通常,
asizeof
memit
慢,然而,在你分析小物件時,
asizeof
會是有用的。
memit
許對現實世界的應用程式比較有用,因為行程的實際記憶體使用量被量測,而不是被
推斷。
位元組 vs. Unicode
切換到 Python 3.3+ 的一個必要理由就是:它的 Unicode 物件儲存遠比 Python 2.7 經濟
許多。如果你的主要工作是處理大量的字串,而且它們耗用了大量的 RAM,請務必考
慮使用 Python 3.3+,你會自動省下龐大的 RAM
在範例 11-7 中,可以看到,100,000,000 個字元的序列被建構為位元組群集(同 Python
2.7
str
)以及 Unicode 物件。Unicode 版本多花了 4 倍的 RAM,每個 Unicode 字元耗
費了相同的較高代價,不管描述底層資料所需的位元組數量為何。
範例
11-7 Unicode
物件在
Python 2.7
中相當昂貴
In [1]: %load_ext memory_profiler
In [2]: %memit b"a" * int(1e8)
peak memory: 100.98 MiB, increment: 80.97 ...
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