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程式設計
multiprocessing 模組
|
245
範例
9-12 check_prime_in_range
def check_prime_in_range((n, (from_i, to_i))):
if n % 2 == 0:
return False
assert from_i % 2 != 0
for i in xrange(from_i, int(to_i), 2):
if n % i == 0:
return False
return True
對「小非質數」的案例來說,透過
Pool
的驗證時間是 0.1 秒,遠超過 Serial 解法原有的
0.000002 秒。或許,我們可以接受「僅僅一個較慢的結果不是什麼問題」 —但萬一我們
有大量的「小非質數」需要檢查呢?還好,接下來要探討的 Less Naive Pool 解法可以幫
助我們避開這個問題。
Less Naive Pool 解法
前一個解法在驗證較小的非質數時是缺乏效率的,對任何比較小的(少於 18 位數字)
非質數來說,由於傳送被切割之工作的額外成本,以及不知道是否有非常小的因數(比
較可能出現)會被找到,運作起來可能比循序方法還要慢。如果小因數被發現,程式還
是必須等待其他較大因數的搜尋完成。
我們可以開始在行程之間以信號表明小因數已經被找到,但因為這經常發生,所以會增
加許多額外的溝通成本。相反地,範例 9-13 呈現的是比較務實的解法—先針對可能的小
因數快速進行循序檢查,假如沒有找到任何因數,平行的搜尋隨即被展開。在發動相對
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