Time profiling in python using cProfile

In this article we will cover following points:

  1.  Profile single function
  2. Profile complete python script
  3. How to read .prof file
  4. Profile web2py application

To install cprofile on ubuntu use following command:

1) Profile single function

To profile function, import cProfile

Then call function using cProfile.run()

This will write profiling data in test.profile .


Later in this post we will see how to read .profile file

2) Profile python script

Run python script using following command :

This will profile complete script and write profiling data in test.profile.

3) How to read .profile file?

Using pstats module:

.profile file is in binary format. So to examine data from .profile file use the interactive mode of the pstats module by running it as a script with the profile data file as argument.

Now you will go in interactive profile statistics browser.

Now use stats command to display stats

You can sort stats using following sort keys:
cumulative — cumulative time
module — file name
ncalls — call count
pcalls — primitive call count
file — file name
line — line number
name — function name
calls — call count
stdname — standard name
nfl — name/file/line
filename — file name
cumtime — cumulative time
time — internal time
tottime — internal time

For example:

Above command will sort records using tottime ( internal time)

To show particular number of records use command “stats count”

For example:

This will show only 10 records.

Other method to read .profile file is using command “cprofilev -f test_func.profile”

Run above command in terminal and view stats in browser on

Advantage of this method is you can easily sort records ,just by one click on column name.

4. Profile web2py application

Create a empty text file in a directory and then pass this text file as argument to web2py. All profiling data will be stored in this file.
For example :
Create lcm.txt in ‘/home/gaurav/temp/lcm/” directory. Now run web2py server using following command with command line argument -F (profiler filename)

Note: For some web2py version , we have to provide directory using -f (not -F) option and data will be written in that directory in the form of .profile files. We can read these files using above mentioned methods.

Now open application from browser and open pages you want to profile.Then stop web2py server. This will write profiling data to lcm.txt. Data in text file will be in readable format , no need of pstats module to read data.

About Gaurav Vichare

Hi , I am Gaurav : author of this blog , Software engineer and a Photoshop lover . You can connect me on LinkedIn, cirlce me on Google or follow on Twitter