Skip to content

Instantly share code, notes, and snippets.

@Dowwie
Forked from hemanth/fast.md
Created January 27, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dowwie/5a3915966f328f94d87d76ae1385c049 to your computer and use it in GitHub Desktop.
Save Dowwie/5a3915966f328f94d87d76ae1385c049 to your computer and use it in GitHub Desktop.
Guido van Rossum's Tips for fast Python

Guido van Rossum11 Sep 2012 - Public

Some patterns for fast Python. Know any others?

  • 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.

  • Don't write Java (or C++, or Javascript, ...) in Python.

  • Are you sure it's too slow? Profile before optimizing!

  • The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment