Profiling in Python

08 Nov, 2019
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:
# importing time moduleimport time  start = time.time()print("Time Consumed")print("% s seconds" % (time.time() - start))
Time Consumed
0.01517796516418457 seconds

Example #2:
# importing time moduleimport time  def gfg():    start = time.time()    print("Time consumed")    end = time.time()    print("gfg() function takes", end-start, "seconds")  # Calling gfggfg()
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:
# importing line_profiler modulefrom line_profiler import LineProfiler  def geek(rk):    print(rk)  rk ="geeks"profile = LineProfiler(geek(rk))profile.print_stats()
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 information about how long the program is executing and how many times the function get called in a program.
Code #1
# importing cProfileimport cProfile  cProfile.run("10 + 10")
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.
# importing cProfileimport cProfile  def f():    print("hello")cProfile.run('f()')
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}

shubham_singh
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts