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程式設計
編譯成 C
|
141
在下列範例中,
v
是浮點數,或者是代表
complex
的一對浮點數。兩種情況可能在不同的
時間點發生在相同的迴圈裡,或者在相關的連續程式碼區段中︰
v = -1.0
print type(v), abs(v)
<type 'float'> 1.0
v = 1-1j
print type(v), abs(v)
<type 'complex'> 1.41421356237
abs
函式根據底層的資料型別不同而以不同的方式運作。對整數或浮點數來說,
abs
只是
把負值變成正值。就複數而言,
abs
將實數部與虛數部的分量平方和取根號︰
abs(c) = c.real
2
+ c.imag
2
complex
範例的機器碼牽涉到較多指令,並且需要花更長時間來執行。在對某個變數呼
abs
之前,Python 必須先檢查該變數的型別,然後決定要呼叫哪個版本的函式—當你
反覆進行大量呼叫時,這個額外成本會迅速累加起來。
Python 裡,每個基本型別物件(如整數)會被包裹在高階的 Python 物件(如代表
整數的
int
),這個高階物件具有額外的函式,例如,
__hash__
協助儲存,
__str__
處理
列印。
在計算密集型的程式碼區段中,變數型別通常不改變,這讓我們有機會做靜態編譯,以
及較快速的程式碼執行。
如果我們想要的只是進行大量的中間數學運算,就不需要高階的函式,而且我們可能也
不需要參考計數(reference counting)的機制,我們可以降低到機器碼的層級,並且使
用機器碼與位元組迅速進行計算,而不是操作較高階的 ...
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