Optimizing your python code: code performance profiling

Post date: Nov 1, 2016 9:14:47 PM

The first step on optimizing the python performance is to profile and analyze which parts of your code are the bottleneck.

This stackoverflow post elaborated several packages we can use. I ended up using two methods: 1) cProfile (and pstats package) which is quite a standard package in python and 2) PyCallGraph to profile my code. To use PyCallGraph, you will need to:

  1. install pycallgraph package
      1. > pip install pycallgraph
  1. then install graphviz using this command (refer to this post)
      1. > brew install graphviz

Now you can run this command in terminal to profile your code, and the output graph will be created in the current directory.

> pycallgraph graphviz -- ./mypythonscript.py

Other resources:

Huy Nguyen's blog gathered several good resources.