Python profiling

Introduction

Is your python script running slow? Fear no more! Just follow these few steps, and you should be able to pinpoint the culprit.

There are many ways to do this, this is the setup I use for quick profiling.

Prerequisites

  • pyprof2calltree: pip install pyprof2calltree
  • kcachegrind:
    • Debian/Ubuntu: apt-get install kcachegrind
    • Redhat/Centos: yum install kcachegrind

Collecting profiling data

The test run of the script should be with not too small, not too large amount of data. In my test runs I usually aim for about 10-20 seconds: it’s usually enough to get useful data without waiting too much. Make sure this is appropriate for you. It’s easiest if the script is runnable without any user interaction, then just:

Where (change as appropriate):

  • profile.dat will store the execution profile
  • test.py is the python script

Visualizing results

Sometimes it does not work (I saw it on a centos system), then try to do it in 2 steps:

It may be overwhelming at first, but there are enough views to finally figure out what is what. Just play a bit with it!

Tips

  1. When using libraries the graphs will quickly get too detailed (as they show the internal library details). There are options to limit this if it get’s too distracting
  2. Too complex code may get confusing for kcachegrind. If so, try to profile a simpler script with just just as little of the bottleneck code as necessary.