Profiling in Python 3(In web)

Created By: Abdul at Jan. 3, 2021

Finding the place and cause of the problem is very import before trying to implement the solution. If a program running too slowly or using too much RAM, then you will want to fix whichever parts of your code are responsible. You could, of course, directly try to fix what you believe might be the problem, but be careful, as you will often end up fixing the wrong thing. Rather than using your intuition, it is far more sensible to have defined some hypothesis, or at least defined a direction towards the problem before making changes to the structure of your code.

Video explanation about this tutorial:





Here's the concept of profiling comes into the place. Profiling lets us find the bottlenecks, so we can do the least amount of work to get the biggest performance gain. Practically you will aim for your code to run "fast enough" and "lean enough" to fit your needs. Profiling will let you make the most pragmatic decisions for the least efforts. Any measurable resource can be profiled not just the CPU time and memory usage.


In this tutorial, we will explore different options available for profiling in Python with the focused on CPU time and memory usage, but you could apply similar techniques to measure network bandwidth and disk I/O.

Python provides many excellent modules to measure the statistics of your program. This makes us know where the program is spending too much time and what to do in order to optimize that particular part.Se, below are some options for Profiling in python.

  1. Timers
  2. cProfile
  3. Line Profiler

1. Timers:

    Timers are easy to implement and they can be used anywhere in the program to measure the execution time. By using the Timers, we can get     the exact time and can improve the program where it takes too long. Let's write a very simple example of how to use Timers in Python.

import time
def myfunc():
    a = 5 + 3
    b = 4 + 4
    d = (a + b)/b
    return d

start = time.time()
end = time.time()
print('Time consumed: {} secs'.format(end - start))


2. cProfile:

It's a built-in profiling tool in the standard library. It hooks into the virtual machine to measure the time taken to run the function that it sees. This introduces a greater overhead, but you got correspondingly more information. Sometimes, additional information can lead to surprising insights into your code. CProfile module provides all information about how long the program is executing and how many times the function gets called in a program.
Let's do some practice with cProfile.

import cProfile

def f():
    print("Hello world!")'f()')

3. Line Profiler:

LineProfiling is the strongest tool for identifying the cause of CPU-bound problem in python code. It works by profiling individual functions on line-by-line basis. So, you should start with cProfile and use the high-level view to guide which function to profile with LineProfiler. It's worthwhile printing and annotating versions of the output from this tool as you modify your code. So you have a record of changes ( successful or not ) that you can quickly refer to.
You can install it by using pip command as:

 pip install line_profiler 


and here's an example of Line profiler:

from line_profiler import LineProfiler 

def rock(rk):

rk = "Hello world"
profile = LineProfiler(rock(rk))


Post comment below if you have query regarding Profiling in Python, Thank You!

Post a comment

© 2021 | All rights reserved | Design by W3layouts.