Upload
daker-fernandes
View
2.207
Download
0
Embed Size (px)
Citation preview
Why is Pythonslow?Daker Pinheiro
Why is CPythonslow?Daker Pinheiro
$ whois dakerfpDaker Fernandes Pinheiro
UFPEINDT RecifeWebKit (Nix)Qt, KDE, ...C++, C, Python, Javascript, Prolog, ...Pythonist since 2009
InterpretedArchitecture 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 * 100vs
100 ** 4
dict()vs{}
Benchmark>>> import timeit>>> timeit.timeit("[i * i for i in xrange(100)]")
ConcurrencyGlobal Interpreter LockerAvoid ThreadsTry Event LoopsTry Multiprocess
Know your Data StructuresTime Complexity
Use C/C++ BindingsnumpyPyQt, 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 3Similar to Python 2.7 performance
Python 3 - Mailing list
Cythoncdef average(int a, int b): return (a + b) / 2.0
Psyco
Dead, RIP
import psycopsyco.full()
Create C/C++ BindingsPython.hSIPBoost.PythonShiboken
Optimization Checklist1. Legibillity2. Architecture3. Algorithm4. Memory5. Buffering6. Caching7. IO8. Consider other languages :-(