Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fast Python, Slow Python by Alex Gaynor
Search
PyCon 2014
April 12, 2014
Programming
5
2.5k
Fast Python, Slow Python by Alex Gaynor
PyCon 2014
April 12, 2014
Tweet
Share
More Decks by PyCon 2014
See All by PyCon 2014
Postgres Performance for Humans by Craig Kerstiens
pycon2014
29
3.6k
Technical Onboarding, Training, and Mentoring by Kate Heddleston and Nicole Zuckerman
pycon2014
1
2.3k
"My big gay adventure. Making, releasing and selling an indie game made in python." by Luke Miller
pycon2014
2
1.5k
Farewell and Welcome Home, Python in Two Genders by Naomi_Ceder
pycon2014
1
710
Deliver Your Software in an Envelope by Augie Fackler and Nathaniel Manista
pycon2014
1
520
Hitchhikers Guide to Free and Open Source Participation by Elena Williams
pycon2014
6
1.2k
Localization Revisted (aka. Translations Evolved) by Ruchi Varshney
pycon2014
0
690
Smart Dumpster by Bradley E. Angell
pycon2014
0
490
Software Engineering for Hackers: Bridging the Two Solitudes by Tavish Armstrong
pycon2014
0
710
Other Decks in Programming
See All in Programming
MCP with Cloudflare Workers
yusukebe
2
220
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
700
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
300
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
Embracing the Ebb and Flow
colly
84
4.5k
The Cost Of JavaScript in 2023
addyosmani
45
7k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Being A Developer After 40
akosma
87
590k
Automating Front-end Workflow
addyosmani
1366
200k
GraphQLとの向き合い方2022年版
quramy
44
13k
Gamification - CAS2011
davidbonilla
80
5.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Faster Mobile Websites
deanohume
305
30k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Transcript
Fast Python, Slow Python Alex Gaynor
About me • Core developer of Django, PyPy, CPython, etc.
• Director of the Python Software Foundation • Work at Rackspace (Thanks!)
A talk about two things
What is performance?
Why do we care about performance?
Lies, damned lies, and benchmarks
Performance is specialization.
Systems performance
What is Python?
What Python Isn’t • Cython • C • Numba •
RPython
Excuses time
Dynamic typing
You can monkey patch anything
Slow vs Harder to optimize
PyPy
$ python! >>> print 2! 2! $ pypy! >>>> print
2! 2!
Let’s make a deal
How can we specialize our code?
Let’s talk about C
struct Point {! double x;! double y;! double z;! };!
class Point(objet):! def __init__(self, x, y, z):! self.x = x!
self.y = y! self.z = z!
point = {! "x": x,! "y": y,! "z": z,! }!
Dictionaries vs. Objects
std::unordered_map<std::string, double> point;! point["x"] = x;! point["y"] = y;! point["z"]
= z;!
Why don’t we care?
if [hex, bytes, bytes_le, fields, int].count(None) != 4:! raise TypeError('need
exactly one argument')!
if [hex, bytes, bytes_le, fields, int].count(None) != 4:! raise TypeError('need
exactly one argument')!
if (! (hex is None) + (bytes is None) +
(bytes_le is None) +! (fields is None) + (int is None)! ) != 4:! raise TypeError!
Python makes everything easier
Let’s talk about strings
char *data = malloc(1025);! while (true) {! size_t n =
read(fd, data, 1024);! data[n] = '\0';! char *start = data;! while (start < data + n) {! if (isspace(*start)) {! break;! }! start++;! }! printf("%s\n", start);! }!
while True:! data = os.read(fd, 1024)! print data.lstrip()!
Zero Buffer
from zero_buffer import Buffer! ! b = Buffer.allocate(8192)! with open(path,
"rb") as f:! b.read_from(f.fileno())! for part in b.view().split(b":"):! part.write_to(sys.stdout.fileno())! sys.stdout.write('\n')!
https://zero-buffer.readthedocs.org/ https://warehouse.python.org/project/zero_buffer/
A few more myths • Functions calls are really expensive
• Using only builtin data types will make your code fast • Don’t write Python in the style of Java or C
One Python
I hate heuristics
Let’s take a lesson…
Thank you! Questions? https://speakerdeck.com/alex/