%lprun is a line-by-line profiler for Python code that can help you identify performance bottlenecks in your code. It is part of the line_profiler package and can be used in Jupyter notebooks or from the command line.
When you run %lprun on a function, it will show you how much time each line of code takes to execute, as well as other metrics such as memory usage.
Here is an example of what %lprun output might look like:
Timer unit: 1e-06 sTotal time: 0.025097 sFile: example.pyFunction: my_function at line 5Line # Hits Time Per Hit % Time Line Contents============================================================== 5 def my_function(x, y): 6 10 2324.0 232.4 9.3 result = [] 7 1010 8210.0 8.1 32.7 for i in range(x): 8 1000 5805.0 5.8 23.1 row = [] 9 1001000 1233758.0 1.2 4918.8 for j in range(y): 10 1000000 1027955.0 1.0 4092.7 row.append(i*j) 11 1000 6725.0 6.7 26.8 result.append(row) 12 13 10 7.0 0.7 0.0 return result |
In this example, the output shows that most of the time (49.2%) is spent in the inner loop at line 9, where the multiplication operation takes place. This suggests that optimizing this operation could have a significant impact on the performance of the function.
To use %lprun in your own code, you first need to install the line_profiler package. You can then decorate the function you want to profile with @profile, and run the %lprun magic command on the decorated function.
For example:
!pip install line_profiler@profiledef my_function(x, y): ... %lprun -f my_function my_function(x, y) |
This will generate a detailed report of the function's performance, line by line.