Tag Archives: profilers

using python profiler

29 Jun

I am new to python and as I part of my current assignment I had to make multiple DB calls, internal function calls with perforce calls too. This program took a lot of time and the idea was to find the culprit and fix it. The layman approach was to add time statements and track end-start time for each function was that would create a lot of mess in the code and would not be a good way of doing it. Then I realized about profilers( I dont know how come it came to my mind), and I read about it. Profilers seem to me as a perfect way of identifying what all functions consume most of the CPU clocks.

I read about profiling in python which I would say is pretty neat document, you can find it here

I used cProfile over profile for two reasons

  1. I had to pass class object with multiple params, which I was not able to find by profile module
  2. cProfile has less overhead than profile module

to use cProfile, do :


import cProfile

class Class:

def doSomething(self, x):

print "Hello World!"

def startNow(self):

a = 4

cProfile.runctx('self.doSomething(a)',globals(),locals(),<filename_to_dump>)

Thats it :)

now to read the status do


import pstats

p = pstats.Stats(<filename_to_dump>)

p.strip_dirs().sort_stats(-1).print_stats()

you can do much more with sort,like :

sort_stats(‘name’) : sorts by function name

sort_stats(‘time’) : sorts according to time spent

I hope this help others too :)

Follow

Get every new post delivered to your Inbox.