Profiling in Python


In Python profiling is the measure of performance of different parts of a program to check and identify the areas of optimization and bottlenecks. We have many tools to perform profiling for the python code which includes built in modules, libraries and IDEs (Integrated development environments). There are different types of profiling of the python code, let’s see them one by one.

Using Line profiling

Line profiling is the technique used to measure the execution time of each individual line of the program. It helps us to identify which line is taking more execution time and to identify the tight loops or other critical performance parts of the code. In python to perform the Line profiling we have the tool line_profiler.

First we have to install the Line_profiler tool in our python environment by using the below line of code.

pip install line_profiler

Output

Collecting line_profilerNote: you may need to restart the kernel to use updated packages.

  Downloading line_profiler-4.0.3-cp39-cp39-win_amd64.whl (83 kB)
     ---------------------------------------- 83.6/83.6 kB 1.6 MB/s eta 0:00:00
Installing collected packages: line_profiler
Successfully installed line_profiler-4.0.3

Now to check the profiling of the given python code by using the line_profiler tool the below is the syntax.

import line_profiler
line_profiler(python_code)

Example

In this example we will check how much time each line of code is taking to execute by using the line_profiler() tool. This module takes the python code as the input and returns the time as the output by using the print_stats() function.

from line_profiler import LineProfiler
def add(a,b):
   out = a + b
   print("The sum of a and b:", out)
out = LineProfiler(add(10,30))
out.print_stats()

Output

Using Function profiling

Using Function profiling

Function profiling is a technique that allows you to measure the execution time of individual functions or methods in the program. This can help us to identify which functions are taking the most time to execute, and also useful for optimizing code at a higher level. One built-in module for function profiling in Python is cProfile.

The run() function of the cProfile module calculates the time taken by the user defined function to execute.

Syntax

The following is the syntax for using the cProfile.run().

cProfile.run(function_name)

Example

In this example we are trying to pass the function name in the string format to the run() function of the cProfile module and then it returns all the execution statistics.

import cProfile
a = input("Enter the text to be displayed: ")
def display(a):
   print(a)
cProfile.run('display')

Output

Enter the text to be displayed: Welcome to Tutorialspoint.com
         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 <string>:1(<module>)
        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}

Using Memory Profiling

Memory profiling is the other technique that helps the user to measure the amount of memory used by the program over the execution time. This can help us to identify memory leaks or other memory related issues which affects the performance of our code.

We have the function profile() in memory_profiler module which performs the measurement of the memory usage.

If we are using this module for the first time we have to install it in our python environment with the below code.

pip install memory_profiler

After installing the message will be displayed. Collecting memory_profiler

Downloading memory_profiler-0.61.0-py3-none-any.whl (31 kB)
Requirement already satisfied: psutil in c:\users\niharikaa\anaconda3\lib\site-packages (from memory_profiler) (5.9.0)
Installing collected packages: memory_profiler
Successfully installed memory_profiler-0.61.0

Syntax

The syntax for using the memory_profiler is as follows.

python -m memory_profiler file_name
mprof run file_name

Example

Here if we want to get the memory profiling statics of the executed code, we have to create a python file and then need to execute in the command prompt by passing the python file along with the memory_profiler.

The below is the code saved in the python_sample.py file.

from memory_profiler import profile
@profile
def display():
   a = "Welcome to the Tutorialspoint website"
   print(a)
display()

We need to execute the below mentioned line of code in the command prompt to get the memory statics

mprof run python_sample.py

Output

mprof: Sampling memory every 0.1s
running new process
running as a Python program...

Updated on: 19-Oct-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements