Fast Python, Slow Python by Alex Gaynor

Fast Python, Slow Python by Alex Gaynor

D21717ea76044d31115c573d368e6ff4?s=128

PyCon 2014

April 12, 2014
Tweet

Transcript

  1. Fast Python, Slow Python Alex Gaynor

  2. About me • Core developer of Django, PyPy, CPython, etc.

    • Director of the Python Software Foundation • Work at Rackspace (Thanks!)
  3. A talk about two things

  4. What is performance?

  5. Why do we care about performance?

  6. Lies, damned lies, and benchmarks

  7. Performance is specialization.

  8. Systems performance

  9. What is Python?

  10. What Python Isn’t • Cython • C • Numba •

    RPython
  11. Excuses time

  12. Dynamic typing

  13. You can monkey patch anything

  14. Slow vs Harder to optimize

  15. PyPy

  16. $ python! >>> print 2! 2! $ pypy! >>>> print

    2! 2!
  17. Let’s make a deal

  18. How can we specialize our code?

  19. Let’s talk about C

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

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

    self.y = y! self.z = z!
  22. point = {! "x": x,! "y": y,! "z": z,! }!

  23. Dictionaries vs. Objects

  24. std::unordered_map<std::string, double> point;! point["x"] = x;! point["y"] = y;! point["z"]

    = z;!
  25. Why don’t we care?

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

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

    exactly one argument')!
  28. if (! (hex is None) + (bytes is None) +

    (bytes_le is None) +! (fields is None) + (int is None)! ) != 4:! raise TypeError!
  29. Python makes everything easier

  30. Let’s talk about strings

  31. 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);! }!
  32. while True:! data = os.read(fd, 1024)! print data.lstrip()!

  33. Zero Buffer

  34. 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')!
  35. https://zero-buffer.readthedocs.org/ https://warehouse.python.org/project/zero_buffer/

  36. 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
  37. One Python

  38. I hate heuristics

  39. Let’s take a lesson…

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