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程式設計
248
|
第九章
check_every = CHECK_EVERY
if n % i == 0:
value.value = FLAG_SET
return False
return True
在這段程式碼裡,1,000 次迭代的檢查係透過
check_every
區域計數器來達成。事實上,
這種方法雖然可讀性不錯,但對速度來說並不理想。在本節結束前,我們會將它替換成
可讀性較差但速度快很多的方法。
你可能對我們檢查共用旗標的總次數感到好奇。在兩個大質數的案例中,在使用 4 個行
程的情況下,我們總共檢查旗標 316,405 次(所有後續範例都是檢查這麼多次)。因為
每次檢查都有鎖定的成本,累積起來確實相當可觀。
使用 Redis 作為旗標機制
Redis 是記憶體內的鍵∕值對儲存引擎,它提供自己的鎖定機制,而且每個操作都是不
可切割的(atomic),因此,我們不必在 Python(或其他語言)裡操心鎖定的使用。
透過 Redis,我們讓資料儲存機制變成是語言無關的—任何與 Redis 互動的語言或工具
均能夠以兼容的模式共用資料,你可以同樣簡單地在 PythonRubyC++ PHP 之間
共用資料。你能夠在本地機器或網路上共用資料;要將資料分享給其他機器,只需要改
變僅於
localhost
上共用資料的 Redis 預設。
Redis 讓你儲存︰
字串串列
字串集合
排序的字串集合
字串雜湊
Redis 將一切儲存在 RAM,把快照(snapshot)儲存到磁碟(選擇性地使用日誌機制) ...
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