Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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:

sudo pip install cprofilev

1) Profile single function

To profile function, import cProfile

import cProfile

Then call function using'test()', 'test.profile')

This will write profiling data in test.profile .


import cProfile

def test_func():
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    addition = 0

    for num in numbers:
        addition += num

    return addition

if __name__ == '__main__':'test_func()', 'test.profile')

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

2) Profile python script

Run python script using following command :

python -m cProfile -o test.profile

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.

python -m pstats test.profile

Now you will go in interactive Profile Statistics Browser.

Welcome to the profile statistics browser.

Now use stats command to display stats

Welcome to the profile statistics browser.
test.profile% 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:

Welcome to the profile statistics browser.
test.profile% stats tottime

Above command will sort records using tottime ( internal time)

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

For example:

Welcome to the profile statistics browser.
test.profile% stats 10

This will show only 10 records.

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

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)

python -p 8000 -a g0987 -F /home/gaurav/temp/lcm/lcm.txt

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.

The post Time profiling in python using cProfile appeared first on Gaurav Vichare.

This post first appeared on Gaurav Vichare -, please read the originial post: here

Share the post

Time profiling in python using cProfile


Subscribe to Gaurav Vichare -

Get updates delivered right to your inbox!

Thank you for your subscription