664
edits
(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> | |||
#!/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 | |||
A = np.random.rand(1000, 1000) | |||
B = np.random.rand(1000, 1000) | |||
# Perform matrix multiplication | |||
C = np.dot(A, B) | |||
if __name__ == "__main__": | if __name__ == "__main__": | ||
for i in range(100): | |||
matrix_operations() | |||
</syntaxhighlight> | |||
# Run the script: | # Run the script: | ||
<syntaxhighlight lang=bash> | |||
chmod +x script.py | chmod +x script.py | ||
reset; kernprof -l -v script.py | reset; kernprof -l -v script.py | ||
</syntaxhighlight> | |||
Example output: | Example output: | ||
<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 # | 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> | |||
edits