pyparis2017 / performant python, by burkhard kloss

107
Performant Python how to write good, fast, python code @georgebernhard [email protected]

Upload: pole-systematic-paris-region

Post on 21-Jan-2018

83 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: PyParis2017 / Performant python, by Burkhard Kloss

Performant Pythonhow to write good, fast, python code

@[email protected]

Page 2: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 3: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python codehow to write good, fast, python code

Page 4: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python codehow to write good, fast, python codehow to write good, fast, python code

Page 5: PyParis2017 / Performant python, by Burkhard Kloss

import thisBeautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Page 6: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python codehow to write good, fast, python codehow to write good, fast, python code

Page 7: PyParis2017 / Performant python, by Burkhard Kloss

https://twitter.com/jakevdp/status/850373744644182017\

Page 8: PyParis2017 / Performant python, by Burkhard Kloss

Programmers are expensive

Computers are cheap

Page 9: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python codehow to write good, fast, python codehow to write good, fast, python code

Page 10: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 11: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 12: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 13: PyParis2017 / Performant python, by Burkhard Kloss
Page 14: PyParis2017 / Performant python, by Burkhard Kloss

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.

Page 15: PyParis2017 / Performant python, by Burkhard Kloss

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

Page 16: PyParis2017 / Performant python, by Burkhard Kloss

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Donald Knuth

Page 17: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 18: PyParis2017 / Performant python, by Burkhard Kloss

Profiling

Page 19: PyParis2017 / Performant python, by Burkhard Kloss
Page 20: PyParis2017 / Performant python, by Burkhard Kloss

Batteries included!

python -m cProfile anagram0.py > profile_anagram0.txt

Page 21: PyParis2017 / Performant python, by Burkhard Kloss
Page 22: PyParis2017 / Performant python, by Burkhard Kloss
Page 23: PyParis2017 / Performant python, by Burkhard Kloss
Page 24: PyParis2017 / Performant python, by Burkhard Kloss
Page 25: PyParis2017 / Performant python, by Burkhard Kloss
Page 26: PyParis2017 / Performant python, by Burkhard Kloss
Page 27: PyParis2017 / Performant python, by Burkhard Kloss
Page 28: PyParis2017 / Performant python, by Burkhard Kloss
Page 29: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 30: PyParis2017 / Performant python, by Burkhard Kloss
Page 31: PyParis2017 / Performant python, by Burkhard Kloss
Page 32: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 33: PyParis2017 / Performant python, by Burkhard Kloss
Page 34: PyParis2017 / Performant python, by Burkhard Kloss
Page 35: PyParis2017 / Performant python, by Burkhard Kloss

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Donald Knuth

Page 36: PyParis2017 / Performant python, by Burkhard Kloss
Page 37: PyParis2017 / Performant python, by Burkhard Kloss
Page 38: PyParis2017 / Performant python, by Burkhard Kloss
Page 39: PyParis2017 / Performant python, by Burkhard Kloss
Page 40: PyParis2017 / Performant python, by Burkhard Kloss
Page 41: PyParis2017 / Performant python, by Burkhard Kloss
Page 42: PyParis2017 / Performant python, by Burkhard Kloss
Page 43: PyParis2017 / Performant python, by Burkhard Kloss

how to write good, fast, python code

Page 44: PyParis2017 / Performant python, by Burkhard Kloss
Page 45: PyParis2017 / Performant python, by Burkhard Kloss
Page 46: PyParis2017 / Performant python, by Burkhard Kloss
Page 47: PyParis2017 / Performant python, by Burkhard Kloss
Page 48: PyParis2017 / Performant python, by Burkhard Kloss
Page 49: PyParis2017 / Performant python, by Burkhard Kloss
Page 50: PyParis2017 / Performant python, by Burkhard Kloss
Page 51: PyParis2017 / Performant python, by Burkhard Kloss
Page 52: PyParis2017 / Performant python, by Burkhard Kloss

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Donald Knuth

Page 53: PyParis2017 / Performant python, by Burkhard Kloss

“Premature optimization is the root of all evil, so to start this project I'd better come up with a system that can determine whether a possible optimization is premature or

not."

Page 54: PyParis2017 / Performant python, by Burkhard Kloss
Page 55: PyParis2017 / Performant python, by Burkhard Kloss

Fin.

Page 56: PyParis2017 / Performant python, by Burkhard Kloss
Page 57: PyParis2017 / Performant python, by Burkhard Kloss
Page 58: PyParis2017 / Performant python, by Burkhard Kloss
Page 59: PyParis2017 / Performant python, by Burkhard Kloss
Page 60: PyParis2017 / Performant python, by Burkhard Kloss
Page 61: PyParis2017 / Performant python, by Burkhard Kloss
Page 62: PyParis2017 / Performant python, by Burkhard Kloss
Page 63: PyParis2017 / Performant python, by Burkhard Kloss
Page 64: PyParis2017 / Performant python, by Burkhard Kloss
Page 65: PyParis2017 / Performant python, by Burkhard Kloss
Page 66: PyParis2017 / Performant python, by Burkhard Kloss
Page 67: PyParis2017 / Performant python, by Burkhard Kloss
Page 68: PyParis2017 / Performant python, by Burkhard Kloss

Micro optimisations

Page 69: PyParis2017 / Performant python, by Burkhard Kloss

http://www.freeimageslive.co.uk/free_stock_image/labreatarpitsjpg

Page 70: PyParis2017 / Performant python, by Burkhard Kloss
Page 71: PyParis2017 / Performant python, by Burkhard Kloss
Page 72: PyParis2017 / Performant python, by Burkhard Kloss
Page 73: PyParis2017 / Performant python, by Burkhard Kloss

If you want to see my slides on micro optimisations, you can see them below.

Page 74: PyParis2017 / Performant python, by Burkhard Kloss

Anagrams

Page 75: PyParis2017 / Performant python, by Burkhard Kloss
Page 76: PyParis2017 / Performant python, by Burkhard Kloss
Page 77: PyParis2017 / Performant python, by Burkhard Kloss

anagram0.py

+ works+ didn’t take long to write

Page 78: PyParis2017 / Performant python, by Burkhard Kloss
Page 79: PyParis2017 / Performant python, by Burkhard Kloss
Page 80: PyParis2017 / Performant python, by Burkhard Kloss

Python 2.7 FTW!

Page 81: PyParis2017 / Performant python, by Burkhard Kloss

PyPy FTW FTW!

Page 82: PyParis2017 / Performant python, by Burkhard Kloss

52 x faster - Datastructures FTW!

Page 83: PyParis2017 / Performant python, by Burkhard Kloss
Page 84: PyParis2017 / Performant python, by Burkhard Kloss

What are you doing?

Page 85: PyParis2017 / Performant python, by Burkhard Kloss
Page 86: PyParis2017 / Performant python, by Burkhard Kloss
Page 87: PyParis2017 / Performant python, by Burkhard Kloss
Page 88: PyParis2017 / Performant python, by Burkhard Kloss
Page 89: PyParis2017 / Performant python, by Burkhard Kloss
Page 90: PyParis2017 / Performant python, by Burkhard Kloss
Page 91: PyParis2017 / Performant python, by Burkhard Kloss
Page 92: PyParis2017 / Performant python, by Burkhard Kloss

And now for something completely different….

Page 93: PyParis2017 / Performant python, by Burkhard Kloss

Variations on an almost trivial sum….

Page 94: PyParis2017 / Performant python, by Burkhard Kloss
Page 95: PyParis2017 / Performant python, by Burkhard Kloss
Page 96: PyParis2017 / Performant python, by Burkhard Kloss
Page 97: PyParis2017 / Performant python, by Burkhard Kloss
Page 98: PyParis2017 / Performant python, by Burkhard Kloss
Page 99: PyParis2017 / Performant python, by Burkhard Kloss

Tools

Page 100: PyParis2017 / Performant python, by Burkhard Kloss
Page 101: PyParis2017 / Performant python, by Burkhard Kloss
Page 102: PyParis2017 / Performant python, by Burkhard Kloss
Page 103: PyParis2017 / Performant python, by Burkhard Kloss
Page 104: PyParis2017 / Performant python, by Burkhard Kloss
Page 105: PyParis2017 / Performant python, by Burkhard Kloss
Page 106: PyParis2017 / Performant python, by Burkhard Kloss
Page 107: PyParis2017 / Performant python, by Burkhard Kloss