Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyConZA 2012: "Details of Python performance" b...

Pycon ZA
October 05, 2012

PyConZA 2012: "Details of Python performance" by Maciej Fijałkowski

This talk will present how Python runs different constructs, including examples using CPython and PyPy. It'll also show what are the performance characteristics of various constructs and some basic info on how to make your programs run faster.

Pycon ZA

October 05, 2012
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. Python performance characteristics Maciej Fijałkowski PyCon 2012 October 5, 2012

    Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 1 / 22
  2. Who am I? Maciej Fijałkowski (yes this is unicode) PyPy

    core developer for I don’t remember performance freak Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 1 / 22
  3. What this talk is about? python performance (or lack of

    it) why does it matter what can we do about it how PyPy works Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 2 / 22
  4. Python performance message according to Guido “Avoid overengineering datastructures. Tuples

    are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.” “Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.” “Be suspicious of function/method calls; creating a stack frame is expensive.” “The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.” Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 3 / 22
  5. What does it mean? don’t use abstractions don’t use Python

    Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 4 / 22
  6. What does it mean? don’t use abstractions don’t use Python

    Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 4 / 22
  7. But also measure! there are so many variables, you cannot

    care without benchmarks if you have no benchmarks, you don’t care Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 5 / 22
  8. But also measure! there are so many variables, you cannot

    care without benchmarks if you have no benchmarks, you don’t care Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 5 / 22
  9. This is not how I want to write software I

    like my abstractions I like Python I don’t want to rewrite stuff for performance in C/C++ Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 6 / 22
  10. This is not how I want to write software I

    like my abstractions I like Python I don’t want to rewrite stuff for performance in C/C++ Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 6 / 22
  11. Second best keep my abstractions do arcane voodoo to keep

    my programs fast but you have to understand the voodo in the first place Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 7 / 22
  12. But Python performance! there is no such thing as language

    performance there is implementation performance the language might be easier or harder to optimize CPython performance characteristics is relatively straightforward Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 8 / 22
  13. What is PyPy? PyPy is a Python interpreter (that’s what

    we care about) PyPy is a toolchain for creating dynamic language implementations also, an Open Source project that has been around for a while Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 9 / 22
  14. Compilers vs interpreters compilers compile language X (C, Python) to

    a lower level language (C, assembler) ahead of time interpreters compile language X to bytecode and have a big interpreter loop PyPy has a hybrid approach. It’s an interpreter, but hot paths are compiled directly to assembler during runtime Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 10 / 22
  15. Compilers vs interpreters compilers compile language X (C, Python) to

    a lower level language (C, assembler) ahead of time interpreters compile language X to bytecode and have a big interpreter loop PyPy has a hybrid approach. It’s an interpreter, but hot paths are compiled directly to assembler during runtime Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 10 / 22
  16. What is just in time (JIT) compilation? few different flavors

    observe runtime values compile code with agressive optimizations have checks if assumptions still stand Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 11 / 22
  17. So what PyPy does? interprets a Python program the JIT

    observes python interpreter producing code through the path followed by the interpreter compiles loops and functions Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 12 / 22
  18. Some properties the code speed changes over time hopefully from

    slow to fast you need to warm up things before they get fast Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 13 / 22
  19. Abstractions inlining, malloc removal abstractions are cheap if they don’t

    introduce too much complexity Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 15 / 22
  20. Abstractions inlining, malloc removal abstractions are cheap if they don’t

    introduce too much complexity Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 15 / 22
  21. Few words about garbage collection CPython: refcounting + cyclic collector

    PyPy: generational mark & sweep errr.... Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 16 / 22
  22. Few words about garbage collection CPython: refcounting + cyclic collector

    PyPy: generational mark & sweep errr.... Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 16 / 22
  23. The rest I’ll explain various PyPy strategies ideally all this

    knowledge will be unnecessary this is the second best, how to please the JIT compiler Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 17 / 22
  24. Allocations (PyPy) allocation is expensive for a good GC, short

    living objects don’t matter it’s better to have a small persistent structure and abstraction on allocation copying however is expensive we have hacks for strings, but they’re not complete Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 18 / 22
  25. Allocations (PyPy) allocation is expensive for a good GC, short

    living objects don’t matter it’s better to have a small persistent structure and abstraction on allocation copying however is expensive we have hacks for strings, but they’re not complete Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 18 / 22
  26. Calls Python calls are an incredible mess simple is better

    than complex simple call comes with no cost, the cost grows with growing complexity Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 19 / 22
  27. Attribute access if optimized, almost as fast as local var

    access dict lookup optimized away class attributes considered constant meta programming is better than dynamism objects for small number of constant keys, dicts for large numbers of changing keys Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 20 / 22
  28. Other sorts of loops there is more! tuple(iterable), map(iterable), re.search

    they’re all jitted not all nicely Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 21 / 22
  29. Summary we hope this knowledge will not be needed the

    more you care, the better you need to know Maciej Fijałkowski (PyCon 2012) Python performance characteristics October 5, 2012 22 / 22