Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
最佳化
|
541
時間因此正比於
1+2+3+...N-1
,其總和值為
O(N
2
)
。因此,
slow
的總執行時
間為
O(N
2
)
以一個
O(N)
的解法取代
O(N
2
)
的解法幾乎總是值得的,除非你能以某種
方式為輸入大小
N
設下嚴格的小值限制。如果
N
的增長沒有非常嚴格的界
限,那麼最後大型的
N
值會讓
O(N
2
)
的解法比
O(N)
的解法慢非常多,不管
每個情況中的比例常數為何(也不管效能側寫告訴了你什麼)。除非其他
地方有你無法消除的其他
O(N
2
)
或更糟的瓶頸,程式中那個是
O(N
2
)
的部分
就會是程式的主要瓶頸,占據了大型
N
值大部分的執行時間。幫你自己一
個忙,留意這種 big O:所有其他的效能議題,相較之下,通常幾乎都可以
算是無關緊要。
順便提一下,你還可以讓函式
fast
變得更快,方法是用 Python 的慣用語
來表達它。只需以下列這單一個述句取代頭兩行:
result = list(it)
這個變更不會影響到
fast
big-O 特徵(
fast
在變更後仍為
O(N)
),但會
以一個很大的常數倍數加速執行。
簡單比複雜好,通常也比較快!
Python 中,常見的情形是,表達事情的時候,最簡單、
最清楚、最直接且慣用語式(idiomatic)的方式也會是最
快的。
Python 中,挑選 big-O 好的演算法的工作大致就跟在任何其他語言中相
同。你只需要 Python 基本構建組塊(building blocks)的 big-O 效能相關
的幾個線索,而我們會在接下來的章節提供它們。
串列作業
Python 的串列在內部實作為 ...
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

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival

Publisher Resources

ISBN: 9789864766819