Why is Python slow? Python Nordeste 2013

Post on 06-May-2015

2.214 views 0 download

Transcript of Why is Python slow? Python Nordeste 2013

Why is Pythonslow?Daker Pinheiro

Why is CPythonslow?Daker Pinheiro

Why is CPython2.x slow?

Daker Pinheiro

$ whois dakerfpDaker Fernandes Pinheiro

UFPEINDT RecifeWebKit (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

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()

PyPy

http://speed.pypy.org/

Create C/C++ BindingsPython.hSIPBoost.PythonShiboken

Optimization Checklist1. Legibillity2. Architecture3. Algorithm4. Memory5. Buffering6. Caching7. IO8. Consider other languages :-(

Q & ADaker Fernandes Pinheiro

http://codevereal.blogspot.com