Profiling in Python

Python provides many excellent modules to measure the statistics of a program. This makes us know where the program is spending too much time and what to do inorder to optimize it. It is better to optimize the code inorder to increase the efficiency of a program. So, perform some standard tests to ensure optimization and we can improve the program inorder to increase the efficiency.

Using Timers:
Timers are easy to implement and they can be used anywhere at a program to measure the execution time. By using timers we can get the exact time and we can improve the program where it takes too long. Time module provides the methods inorder to profile a program.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing time module
import time
  
start = time.time()
print("Time Consumed")
print("% s seconds" % (time.time() - start))

chevron_right


Output:

Time Consumed
0.01517796516418457 seconds

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing time module
import time
  
def gfg():
    start = time.time()
    print("Time consumed")
    end = time.time()
    print("gfg() function takes", end-start, "seconds")
  
# Calling gfg
gfg()

chevron_right


Output:

Time consumed
gfg() function takes 0.015180110931396484 seconds

 
Using line_profiler:
Python provides a built-in module to measure execution time and the module name is LineProfiler.It gives detailed report on time consumed by a program.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing line_profiler module
from line_profiler import LineProfiler
  
def geek(rk):
    print(rk)
  
rk ="geeks"
profile = LineProfiler(geek(rk))
profile.print_stats()

chevron_right


Output:

Timer unit: 4.27198e-10 s

 
Using cProfile:
Python includes a built in module called cProfile which is used to measure the execution time of a program.cProfiler module provides all infromation about how long the program is executing and how many times the function get called in a program.

Code #1

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing cProfile
import cProfile
  
cProfile.run("10 + 10")

chevron_right


Output:

3 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 :1()
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Code #2: cProfile to measure the statistics about any function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing cProfile
import cProfile
  
def f():
    print("hello")
cProfile.run('f()')

chevron_right


Output:

hello
         5 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 3233da5f950795af777f4b63136f7efd.py:5(f)
        1    0.000    0.000    0.000    0.000 :1()
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.