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.4k
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.5k
Technical Onboarding, Training, and Mentoring by Kate Heddleston and Nicole Zuckerman
pycon2014
1
2.1k
"My big gay adventure. Making, releasing and selling an indie game made in python." by Luke Miller
pycon2014
2
1.4k
Farewell and Welcome Home, Python in Two Genders by Naomi_Ceder
pycon2014
1
680
Deliver Your Software in an Envelope by Augie Fackler and Nathaniel Manista
pycon2014
1
510
Hitchhikers Guide to Free and Open Source Participation by Elena Williams
pycon2014
6
1.1k
Localization Revisted (aka. Translations Evolved) by Ruchi Varshney
pycon2014
0
640
Smart Dumpster by Bradley E. Angell
pycon2014
0
450
Software Engineering for Hackers: Bridging the Two Solitudes by Tavish Armstrong
pycon2014
0
680
Other Decks in Programming
See All in Programming
Rethinking UI building strategies @ SFI 2024
letelete
0
270
Apache Hive 4 on Treasure Data
ryukobayashi
0
310
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
700
Ruby Function Composition
bkuhlmann
1
330
見た目から始める生産性向上
ikumatadokoro
7
840
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
8
4.1k
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
27
8.3k
雑に思考を整理する技術と効能
konifar
60
29k
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
110
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.4k
ONE WEDGE_company_guide
1wedge_one
0
480
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
How STYLIGHT went responsive
nonsquared
92
4.8k
Debugging Ruby Performance
tmm1
70
11k
We Have a Design System, Now What?
morganepeng
43
6.8k
Six Lessons from altMBA
skipperchong
21
3k
Become a Pro
speakerdeck
PRO
11
4.5k
Being A Developer After 40
akosma
57
580k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
241
1.2M
How to name files
jennybc
65
93k
Design by the Numbers
sachag
274
18k
It's Worth the Effort
3n
180
27k
Bash Introduction
62gerente
604
210k
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/