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 .

Example:

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 http://127.0.0.1:4000

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.

How to measure performance of code ? – Basic methods

In this article, we will cover some very basic methods to calculate performance of script. In next article we will cover advance tool like cProfile and memory_profiler. What is profiling? Profiling is program analysis that measures, for … [Continue reading]

Size Contest (SPOJ Challenge problem 378)

Given the set of integers, find the sum of all positive integers in it. Solutions can be sent in any language supported by SPOJ except Whitespace. Input t – number of test cases [t < 1000] On each of next t lines given a integer N [-1000 <= … [Continue reading]

Factorial (SPOJ problem 11)

View problem on SPOJ The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the … [Continue reading]

Check whether one string is rotated version of other(Josh software programming test problem-2)

Write a program to accept two strings str1 and str2 and checks whether str2 is rotated version of str1. Ex. Following are different values of str2 for given str1. GIVEN:str1="software" 2-character left rotate str2='resoft' 4-character left … [Continue reading]

Generate graph from input 1-D array (Josh Software programming test problem-1)

Josh software apti

Following program was asked by Josh Software during campus recruitment. Write a program to generate following chart without using any graph library.This is of marks from 1 to 5 obtained by 10 student represented by an array as … [Continue reading]

Remove duplicate characters in string

Write code to remove the duplicate characters in a string without using any additional buffer NOTE: One or two additional variables are fine An extra copy of the array is not. This question is from the book "cracking thecoding interview". Time … [Continue reading]

Recursive insertion sort

gaurav.vichare

[crayon-55bcb1a2b1968027133545/]   … [Continue reading]

Snake game in C language using queue

Requirments: Turbo C++ compiler If you face any graphics error ,follow these steps. Go to "BGI" folder. ( C:\TurboC++\Disk\TurboC3\BGI). Copy file "EGAVGA.BGI"  Got "BIN" folder. ( C:\TurboC++\Disk\TurboC3\BIN). Paste "EGAVGA.BGI" file … [Continue reading]

99 things I learned while blogging

things I learned while blogging

I started my blogging career with blogger.com. Then I migrated to wordpress.org. Now I run 2 blogs on wordpress. In my 7 months of short blogging career I learned many thing .  I am sharing all points with you. Set your goals. Blog about … [Continue reading]