How to use %lprun and %mprun commands to evaluate code efficiency in python

How to use %lprun and %mprun commands to evaluate code efficiency in python

%lprun command

  • Calculate runtime for each line in the code

1.Load line profiler in your IPython session

%load_ext line_profiler

2.Use %lprun commad

%lprun -f function_name function_call

%mprun

  • Calculate memory consumption for each line in the code

1.load memory profile in IPython session

%load_ext memory_profiler

2.import function from file.py

from file import sum

3.Use %mprun commad


%mprun -f function_name function_call

Example

def sum(x , y):
    return x + y

To calculate Runtime for this function

%load_ext line_profiler
%lprun -f sum sum(5,6)

To calculate memory consumption for this function

%load_ext memory_profiler
from file import sum
%mprun -f sum sum(5,6)

See You Later