Profiling in Python using cProfile

|
| By Webner

Many times we need to optimize our code as our program is spending too much time on some specific line of code. There are hundreds of lines of code and we are stuck on which piece of code to modify to increase the efficiency of the code. For, this python provides us many inbuilt modules that we can use. Profiling a Python program means doing the dynamic analysis of time spent on every function. You can get to know where to optimize the code to increase its speed. Here we are using cProfile for Profiling purpose.

Example of cProfile –

import random
def add(num1, num2):
result = float(num1) + float(num2)
return result
def sub(num1, num2):
result = float(num1) - float(num2)
return result
def mul(num1, num2):
result = float(num1) * float(num2)
return result
def div(num1, num2):
result = float(num1) / float(num2)
return result
def main():
sum_list = list()
sub_list = list()
mul_list = list()
div_list = list()
for n in range(0, 200000):
num1 = random.randint(1,100)
num2 = random.randint(1,100)
# Add two numbers
addition_data = add(num1, num2)
sum_list.append(addition_data)
# Subtract two numbers
subtation_data = sub(num1, num2)
sub_list.append(subtation_data)
# Multiply two numbers
multiplication_data = mul(num1, num2)
mul_list.append(multiplication_data)
#Divide two numbers
division_data = div(num1, num2)
div_list.append(division_data)
if __name__ == '__main__':
import cProfile
cProfile.run('main()')

Output –

3712746 function calls in 3.184 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.032 0.032 3.183 3.183 :1()
200000 0.140 0.000 0.140 0.000 pythonProfiling.py:12(mul)
200000 0.136 0.000 0.136 0.000 pythonProfiling.py:16(div)
1 0.838 0.838 3.151 3.151 pythonProfiling.py:21(main)
200000 0.161 0.000 0.161 0.000 pythonProfiling.py:4(add)
200000 0.140 0.000 0.140 0.000 pythonProfiling.py:8(sub)
400000 0.594 0.000 1.303 0.000 random.py:174(randrange)
400000 0.265 0.000 1.568 0.000 random.py:218(randint)
400000 0.493 0.000 0.709 0.000 random.py:224(_randbelow)
1 0.001 0.001 3.184 3.184 {built-in method builtins.exec}
800000 0.169 0.000 0.169 0.000 {method 'append' of 'list' objects}
400000 0.070 0.000 0.070 0.000 {method 'bit_length' of 'int' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
512742 0.146 0.000 0.146 0.000 {method 'getrandbits' of '_random.Random' objects}

Here first line, we get to know the total number of calls made in program and total time taken by the program to complete the execution

Then in table following are showed – ncalls(total calls made for that function), tottime(total time spent), percall(Average time – tottime/ncalls), cumtime(total time spent including calls to other functions), percall (Average time per call – cumtime/ncalls), filename:lineno (the filename, line number and function called)

Leave a Reply

Your email address will not be published. Required fields are marked *