Python Profiling with line profiler

From RidgeRun Developer Wiki

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.
#!/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()

Run the script:

chmod +x script.py
reset; kernprof -l -v script.py


Example output:

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)


For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.


Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.