Why is Python
slow?
Daker Pinheiro
Why is CPython
slow?
Daker Pinheiro
Why is CPython
2.x slow?
Daker Pinheiro
$ whois dakerfp
Daker Fernandes Pinheiro
UFPE
INDT Recife
WebKit (Nix)
Qt, KDE, ...
C++, C, Python, Javascript, Prolog, ...
Pythonist since 2009
Is Python slow?
http://benchmarksgame.alioth.debian.org
Is Python slow?
http://benchmarksgame.alioth.debian.org
Is Python slow?
http://benchmarksgame.alioth.debian.org
Is Python slow?
http://benchmarksgame.alioth.debian.org
Is Python slow?
http://benchmarksgame.alioth.debian.org
Interpreted
Architecture independency
PyObject, PyObjectType &
PyHeapTypeObject
Typeless variables
Virtual Stack Machine
>>> (z * y) + x + z
Virtual Stack Machine
Bytecode Inspection
>>> import dis
>>> dis.dis(lambda x, y, z: (z * y) + x + z)
2 0 LOAD_FAST 2 (z)
3 LOAD_FAST 1 (y)
6 BINARY_MULTIPLY
7 LOAD_FAST 0 (x)
10 BINARY_ADD
11 LOAD_FAST 2 (z)
14 BINARY_ADD
15 RETURN_VALUE
100 * 100 * 100 * 100
vs
100 ** 4
dict()
vs
{}
Benchmark
>>> import timeit
>>> timeit.timeit("[i * i for i in xrange(100)]")
Concurrency
Global Interpreter Locker
Avoid Threads
Try Event Loops
Try Multiprocess
Know your Data Structures
Time Complexity
Use C/C++ Bindings
numpy
PyQt, PySide
...
standard library
[i * i for i in range(100)]]
[i * i for i in xrange(100)]]
[i * i for i in np.arange(100)]]
ar = np.arange(100); ar * ar
Memory
Python 3
Similar to Python 2.7 performance
Python 3 - Mailing list
Cython
cdef average(int a, int b):
return (a + b) / 2.0
Psyco
Dead, RIP
import psyco
psyco.full()
PyPy
http://speed.pypy.org/
Create C/C++ Bindings
Python.h
SIP
Boost.Python
Shiboken
Optimization Checklist
1. Legibillity
2. Architecture
3. Algorithm
4. Memory
5. Buffering
6. Caching
7. IO
8. Consider other languages :-(
Q & A
Daker Fernandes Pinheiro
http://codevereal.blogspot.com

Why is Python slow? Python Nordeste 2013