Python Profiling with line profiler: Difference between revisions

no edit summary
(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...")
 
No edit summary
Line 6: Line 6:
# Create a script that runs through your code in a loop.
# Create a script that runs through your code in a loop.


 
<syntaxhighlight lang=python>
<nowiki><syntaxhighlight lang=python></nowiki>
#!/usr/bin/env python3
 
<nowiki>#</nowiki>!/usr/bin/env python3


import numpy as np
import numpy as np
from line_profiler import profile
from line_profiler import profile


@profile
@profile
def matrix_operations():
def matrix_operations():
 
    # Create two large random matrices
   # Create two large random matrices
    A = np.random.rand(1000, 1000)
 
    B = np.random.rand(1000, 1000)
   A = np.random.rand(1000, 1000)
   
 
    # Perform matrix multiplication
   B = np.random.rand(1000, 1000)
    C = np.dot(A, B)
 
   
 
   # Perform matrix multiplication
 
   C = np.dot(A, B)
 


if __name__ == "__main__":
if __name__ == "__main__":
 
    for i in range(100):
   for i in range(100):
        matrix_operations()
 
</syntaxhighlight>
       matrix_operations()
 
<nowiki></syntaxhighlight></nowiki>


# Run the script:
# Run the script:


 
<syntaxhighlight lang=bash>
<nowiki><syntaxhighlight lang=bash></nowiki>
 
chmod +x script.py
chmod +x script.py
reset; kernprof -l -v script.py
reset; kernprof -l -v script.py
 
</syntaxhighlight>
<nowiki></syntaxhighlight></nowiki>




Example output:
Example output:


<nowiki><syntaxhighlight lang=bash></nowiki>
<syntaxhighlight lang=bash>
 
Wrote profile results to script.py.lprof
Wrote profile results to script.py.lprof
Timer unit: 1e-06 s
Timer unit: 1e-06 s


Total time: 3.7408 s
Total time: 3.7408 s
File: script.py
File: script.py
Function: matrix_operations at line 6
Function: matrix_operations at line 6


Line #      Hits         Time  Per Hit   % Time  Line Contents
Line #     Hits         Time  Per Hit   % Time  Line Contents
 
==============================================================
<nowiki>==============================================================</nowiki>
    6                                           @profile
 
    7                                           def matrix_operations():
    6                                           @profile
    8                                               # Create two large random matrices
 
    9       100   1061370.10613.7     28.4     A = np.random.rand(1000, 1000)
    7                                           def matrix_operations():
    10       100   1140344.11403.4     30.5     B = np.random.rand(1000, 1000)
 
    11                                              
    8                                               # Create two large random matrices
    12                                               # Perform matrix multiplication
 
    13       100   1539081.15390.8     41.1     C = np.dot(A, B)
    9       100    1061370.10613.7     28.4      A = np.random.rand(1000, 1000)
</syntaxhighlight>
 
   10       100    1140344.11403.4     30.5      B = np.random.rand(1000, 1000)
 
   11                                               
 
   12                                               # Perform matrix multiplication
 
   13       100    1539081.15390.8     41.1      C = np.dot(A, B)
 
<nowiki></syntaxhighlight></nowiki>
664

edits