Python Programming | cProfile
cProfile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.
It’s very easy to use and gives us an insight of what might be wrong or part of the code is taking most time, thus helping us a lot in optimizing.
Lets start by creating a dummy function called do_something
and import cProfile
import cProfile# a dummy function for demonstration
def do_something():
li = []
for i in range(100000):
li.append(i)
li.sort()
return li
Now, lets initialize the profiler…
pr = cProfile.Profile()
pr.enable()
do_something()
pr.disable()
pr.print_stats()
This will output us this
Here we can see that it displays number of calls, total time taken and cumulative time of every function calls. We can also save this stats data in a file using pr.dump_stats(filename)
instead of pr.print_stats()
and can sort it and perform other statistical operations using pstats
. Lets have a look at the whole code now.
import cProfile, pstatsdef do_something(): li = [] for i in range(100000): li.append(i) li.sort() return lipr = cProfile.Profile()pr.enable()do_something()pr.disable()pr.dump_stats('prof_data')ps = pstats.Stats('prof_data')ps.sort_stats(pstats.SortKey.CUMULATIVE)ps.print_stats()
This save the stats data in prof_data
file and also displays the stats ordered by cumulative time.
Here is the video tutorial for this