127 points · by dpmehta02 · 11y ago · 43 comments
My personal favourite optimisation, from needing to shave a few milliseconds off our API response times, was discovering that it's measurably slower to use * args and * *kwargs, and switching to explicitly declaring and passing arguments in the relevant parts of the code.
We also did a few other neat things:
- Rolled our own UUID-like generator in pure Python (I was surprised this helped, but the profiler doesn't lie)
- Switched to working directly with WebOb Request and Response objects rather than using a framework
- Used a background thread with a single slot queue to make sure our response was returned to the user before we emitted the event log message, but always emit the message before moving to the next request
- Heavy optimisation of memcache / redis reads and writes
Edit: Fixed formatting
The crosstown_traffic API in hendrix does exactly this.
Dump your virtualenv, create a new one with pypy, reinstall libraries and test your app. Takes less than 20 minutes, even for complex applications.
This is the advantage Python has over lower level languages - easy way to implement complicated things.
Kind of like Linus's quote: "Bad programmers worry about the code. Good programmers worry about data structures and their relationships."
Also, profiling pypy is less straightforward than profiling CPython code since the hotspot changes the runtime characteristics of the program. This means you need to run tests many times over to make sure the code warms up. This makes further optimizations slightly more difficult. It's not a problem for people with experience optimizing Python code, but for people who actually hope to learn something from OP's blog post, it might be a sticking point.
In my experience in using Python for stats, script type work (as opposed to writing servers and daemons), pypy just isn't that useful. All the Python code is doing is gluing numpy and Cython code together and pypy isn't likely to be able to warm up in time to beat it - and it won't beat it since it's spending most of its time in C.
Obviously, if pypy is an ideal choice for you, use it. But I don't think your experience should really be put forward as a general approach.
For example, I am writing code that implements networks that evolve over time for AI research. Prototyping it in Python makes it easy to test things out, but I expect that I will have to rewrite it in C++ or maybe something more fun, like Haskell[1].
1. Mostly for the sheer joy of trolling my colleagues with a learning agent monad.
In my experience the reason why you wrote something in python (implement features faster) remain valid reasons later on when you want to add functionality.
By using pypy, cpython, rewriting small parts in C/C++ and/or using the libraries which are written in C (such as numpy) you can normally make the hotspots in your code fast enough, while keeping the advantages of python.
That's why you must quantify advantages and disadvantages, including risks minimization and only then you'll see, whether given course of action is viable.
Well, most of the time at least. Think about DST and leap seconds.
JRuby is faster than standard C Ruby too.
It IS an interpret itself, and it runs on a virtual machine that takes care of JIT compilation.