Slide 1

Slide 1 text

Fast Python, Slow Python Alex Gaynor

Slide 2

Slide 2 text

About me • Core developer of Django, PyPy, CPython, etc. • Director of the Python Software Foundation • Work at Rackspace (Thanks!)

Slide 3

Slide 3 text

A talk about two things

Slide 4

Slide 4 text

What is performance?

Slide 5

Slide 5 text

Why do we care about performance?

Slide 6

Slide 6 text

Lies, damned lies, and benchmarks

Slide 7

Slide 7 text

Performance is specialization.

Slide 8

Slide 8 text

Systems performance

Slide 9

Slide 9 text

What is Python?

Slide 10

Slide 10 text

What Python Isn’t • Cython • C • Numba • RPython

Slide 11

Slide 11 text

Excuses time

Slide 12

Slide 12 text

Dynamic typing

Slide 13

Slide 13 text

You can monkey patch anything

Slide 14

Slide 14 text

Slow vs Harder to optimize

Slide 15

Slide 15 text

PyPy

Slide 16

Slide 16 text

$ python! >>> print 2! 2! $ pypy! >>>> print 2! 2!

Slide 17

Slide 17 text

Let’s make a deal

Slide 18

Slide 18 text

How can we specialize our code?

Slide 19

Slide 19 text

Let’s talk about C

Slide 20

Slide 20 text

struct Point {! double x;! double y;! double z;! };!

Slide 21

Slide 21 text

class Point(objet):! def __init__(self, x, y, z):! self.x = x! self.y = y! self.z = z!

Slide 22

Slide 22 text

point = {! "x": x,! "y": y,! "z": z,! }!

Slide 23

Slide 23 text

Dictionaries vs. Objects

Slide 24

Slide 24 text

std::unordered_map point;! point["x"] = x;! point["y"] = y;! point["z"] = z;!

Slide 25

Slide 25 text

Why don’t we care?

Slide 26

Slide 26 text

if [hex, bytes, bytes_le, fields, int].count(None) != 4:! raise TypeError('need exactly one argument')!

Slide 27

Slide 27 text

if [hex, bytes, bytes_le, fields, int].count(None) != 4:! raise TypeError('need exactly one argument')!

Slide 28

Slide 28 text

if (! (hex is None) + (bytes is None) + (bytes_le is None) +! (fields is None) + (int is None)! ) != 4:! raise TypeError!

Slide 29

Slide 29 text

Python makes everything easier

Slide 30

Slide 30 text

Let’s talk about strings

Slide 31

Slide 31 text

char *data = malloc(1025);! while (true) {! size_t n = read(fd, data, 1024);! data[n] = '\0';! char *start = data;! while (start < data + n) {! if (isspace(*start)) {! break;! }! start++;! }! printf("%s\n", start);! }!

Slide 32

Slide 32 text

while True:! data = os.read(fd, 1024)! print data.lstrip()!

Slide 33

Slide 33 text

Zero Buffer

Slide 34

Slide 34 text

from zero_buffer import Buffer! ! b = Buffer.allocate(8192)! with open(path, "rb") as f:! b.read_from(f.fileno())! for part in b.view().split(b":"):! part.write_to(sys.stdout.fileno())! sys.stdout.write('\n')!

Slide 35

Slide 35 text

https://zero-buffer.readthedocs.org/ https://warehouse.python.org/project/zero_buffer/

Slide 36

Slide 36 text

A few more myths • Functions calls are really expensive • Using only builtin data types will make your code fast • Don’t write Python in the style of Java or C

Slide 37

Slide 37 text

One Python

Slide 38

Slide 38 text

I hate heuristics

Slide 39

Slide 39 text

Let’s take a lesson…

Slide 40

Slide 40 text

Thank you! Questions? https://speakerdeck.com/alex/