Sitemap
Pranay Gore

Pranay Gore

Learner | Big data & Cloud evangelist | Python developer | Amuser & Original

Press enter or click to view image in full size

Quick on profiling Python code

4 min readMay 14, 2020

--

I am sure you know what is profiling the code mean. If not, then wiki says profiling is a form of dynamic program analysis that measures the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls. Profiling information serves to aid program optimisation.

I personally use it to find the stats of a program, so that I know which part of my code needs attention or optimisation to increase the efficiency.

So when we want to find a bottleneck or the resources your code consumes, we look at 3 things- CPU, MEMORY and NETWORK.

Here, we will mainly focusing on cpu usage.

Let me start something with very basic, which I used to (I still do it) write as a speed test and I guess most of us do that.

CPU usage:

It can be further divided into two parts-

Macro-profiling:

This profiles the whole program and stats are collected when executed. We have tools for this which is a part python standard library.

cProfile (C implenentation) and profile(Python implementation)

Let us try some code.

Press enter or click to view image in full size

We get the following output if use cProfile

(Syntax: python3 -m cProfile testcode.py)

Press enter or click to view image in full size

and below if we use profile.

(Syntax:python3 -m profile testcode.py)

Press enter or click to view image in full size

Note: These syntaxes works well with python 2.7 version too.

Get Pranay Gore’s stories in your inbox

Join Medium for free to get updates from this writer.

We can also read the stats in a Python program. We just need to import the profiler module which will profile the code and another module called pstats which will analyze the data collected by the Python profiler.

Press enter or click to view image in full size
Press enter or click to view image in full size

We can also retrieve data specific to any function using pstats module.

Press enter or click to view image in full size

We can also visualise this profiled data into a dot graph. I tried visualising it using gprof2dot and qcachegrind but I faced issue with installation of these modules on Mac. If you are using linux, things should be fine with you guys.

I am using snakeviz for visualisation.

Press enter or click to view image in full size

When we run snakeviz , a new browser window is launched with the graph.

Press enter or click to view image in full size
Press enter or click to view image in full size

In the icicle visualization style,

  • Functions are represented by rectangles. A root function is the top-most rectangle, with functions it calls below it, then the functions those call below them, and so on.
  • The amount of time spent inside a function is represented by the width of the rectangle. A rectangle that stretches across most of the visualization represents a function that is taking up most of the time of its calling function, while a skinny rectangle represents a function that is using hardly any time at all.

From the above findings, we can say that:

  • When the number of calls is really high and takes up most of the global time, the function or method is probably in a loop. Possible optimization may be done by moving this call to different scope in order to reduce number of operations.
  • When one function is taking very long time, a cache might be a good option, if possible.

Micro-profiling:

This profiles a part of a program. When we are done with macro-profiling, we should try speed testing part of the programs.

timeit: is used to test the execution time or compare small code snippets.

Press enter or click to view image in full size

LineProfiler: One of the profilers to profile single line of a code or functions.

Press enter or click to view image in full size
Press enter or click to view image in full size

You can get the code used above @ profiling_in_python.ipynb . If you wish to profile memory and network too, you can take a look at some tools like-

Memprof, Pympler and objgraph (for memory profiling)

ntop and pathrate (for Network profiling)

Note: To perform memory profiling, you should understand how memory is managed in Python first. You can refer to my article for this.

Thanks for reading and enjoy learning!

--

--

Pranay Gore

Written by Pranay Gore

Learner | Big data & Cloud evangelist | Python developer | Amuser & Original