%mprun is a line-by-line memory profiler for Python code that can help you identify memory usage issues in your code. It is part of the memory_profiler package and can be used in Jupyter notebooks or from the command line.
When you run %mprun on a function, it will show you how much memory each line of code uses, as well as other metrics such as execution time.
Here is an example of what %mprun output might look like:
Filename: example.pyLine # Mem usage Increment Line Contents================================================ 1 29.734 MiB 29.734 MiB @profile 2 def my_function(x, y): 3 29.734 MiB 0.000 MiB result = [] 4 29.734 MiB 0.000 MiB for i in range(x): 5 29.734 MiB 0.000 MiB row = [] 6 29.734 MiB 0.000 MiB for j in range(y): 7 29.734 MiB 0.000 MiB row.append(i*j) 8 29.734 MiB 0.000 MiB result.append(row) 9 29.734 MiB 0.000 MiB return result |
In this example, the output shows the memory usage of each line of code in the my_function function. You can see that the memory usage does not increase significantly during the execution of the function, indicating that it is not using a large amount of memory.
To use %mprun in your own code, you first need to install the memory_profiler package. You can then decorate the function you want to profile with @profile, and run the %mprun magic command on the decorated function.
For example:
!pip install memory_profiler@profiledef my_function(x, y): ... %mprun -f my_function my_function(x, y) |
This will generate a detailed report of the function's memory usage, line by line. You can use this information to identify areas of your code that are using a lot of memory, and optimize your code to reduce memory usage.