Python Profiling with line profiler

From RidgeRun Developer Wiki
Revision as of 19:54, 11 October 2024 by Mtaylor (talk | contribs) (Created page with " <code>line_profiler</code> is a python package that lets you profile a python app line by line with minimal code modifications. # Install the tool: <code>pip3 install line_profiler</code> # On your code, import the package and add the <code>@profile</code> decorator before the methods that you want to profile # Create a script that runs through your code in a loop. <nowiki><syntaxhighlight lang=python></nowiki> <nowiki>#</nowiki>!/usr/bin/env python3 import numpy a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

line_profiler is a python package that lets you profile a python app line by line with minimal code modifications.

  1. Install the tool: pip3 install line_profiler
  2. On your code, import the package and add the @profile decorator before the methods that you want to profile
  3. Create a script that runs through your code in a loop.


<syntaxhighlight lang=python>

#!/usr/bin/env python3

import numpy as np

from line_profiler import profile


@profile

def matrix_operations():

   # Create two large random matrices

   A = np.random.rand(1000, 1000)

   B = np.random.rand(1000, 1000)

   

   # Perform matrix multiplication

   C = np.dot(A, B)


if __name__ == "__main__":

   for i in range(100):

       matrix_operations()

</syntaxhighlight>

  1. Run the script:


<syntaxhighlight lang=bash>

chmod +x script.py

reset; kernprof -l -v script.py

</syntaxhighlight>


Example output:

<syntaxhighlight lang=bash>

Wrote profile results to script.py.lprof

Timer unit: 1e-06 s

Total time: 3.7408 s

File: script.py

Function: matrix_operations at line 6

Line #      Hits         Time  Per Hit   % Time  Line Contents

==============================================================

    6                                           @profile

    7                                           def matrix_operations():

    8                                               # Create two large random matrices

    9       100    1061370.6  10613.7     28.4      A = np.random.rand(1000, 1000)

   10       100    1140344.9  11403.4     30.5      B = np.random.rand(1000, 1000)

   11                                               

   12                                               # Perform matrix multiplication

   13       100    1539081.9  15390.8     41.1      C = np.dot(A, B)

</syntaxhighlight>