Profiling python – the easy way

I have this line I need to look up sometimes. By posting it here, I will always know where to find it:

$ python -m cProfile --sort cumulative script.py

It usually gives me a good indication on which parts of my program that are consuming my precious CPU cycles. If you run it on a large program, you will probably want to pipe that into something like head -40.

Right now I needed it to improve the performance of a problem analyzer i am building. It is part of a larger project that I hope will result in a game released on the App Store before Christmas. But more about that in a later post.