Execute a statement under the line-by-line memory profiler from the memory_profiler module. Usage, in line mode: %mprun -f func1 -f func2 Usage, in cell mode: %%mprun -f func1 -f func2 [statement] code... code...
(self, parameter_s='', cell=None)
| 894 | # A lprun-style %mprun magic for IPython. |
| 895 | @line_cell_magic |
| 896 | def mprun(self, parameter_s='', cell=None): |
| 897 | """ Execute a statement under the line-by-line memory profiler from the |
| 898 | memory_profiler module. |
| 899 | |
| 900 | Usage, in line mode: |
| 901 | %mprun -f func1 -f func2 <statement> |
| 902 | |
| 903 | Usage, in cell mode: |
| 904 | %%mprun -f func1 -f func2 [statement] |
| 905 | code... |
| 906 | code... |
| 907 | |
| 908 | In cell mode, the additional code lines are appended to the (possibly |
| 909 | empty) statement in the first line. Cell mode allows you to easily |
| 910 | profile multiline blocks without having to put them in a separate |
| 911 | function. |
| 912 | |
| 913 | The given statement (which doesn't require quote marks) is run via the |
| 914 | LineProfiler. Profiling is enabled for the functions specified by the -f |
| 915 | options. The statistics will be shown side-by-side with the code through |
| 916 | the pager once the statement has completed. |
| 917 | |
| 918 | Options: |
| 919 | |
| 920 | -f <function>: LineProfiler only profiles functions and methods it is told |
| 921 | to profile. This option tells the profiler about these functions. Multiple |
| 922 | -f options may be used. The argument may be any expression that gives |
| 923 | a Python function or method object. However, one must be careful to avoid |
| 924 | spaces that may confuse the option parser. Additionally, functions defined |
| 925 | in the interpreter at the In[] prompt or via %run currently cannot be |
| 926 | displayed. Write these functions out to a separate file and import them. |
| 927 | |
| 928 | One or more -f options are required to get any useful results. |
| 929 | |
| 930 | -T <filename>: dump the text-formatted statistics with the code |
| 931 | side-by-side out to a text file. |
| 932 | |
| 933 | -r: return the LineProfiler object after it has completed profiling. |
| 934 | |
| 935 | -c: If present, add the memory usage of any children process to the report. |
| 936 | """ |
| 937 | from io import StringIO |
| 938 | from memory_profiler import show_results, LineProfiler |
| 939 | |
| 940 | # Local imports to avoid hard dependency. |
| 941 | from distutils.version import LooseVersion |
| 942 | import IPython |
| 943 | ipython_version = LooseVersion(IPython.__version__) |
| 944 | if ipython_version < '0.11': |
| 945 | from IPython.genutils import page |
| 946 | from IPython.ipstruct import Struct |
| 947 | from IPython.ipapi import UsageError |
| 948 | else: |
| 949 | from IPython.core.page import page |
| 950 | from IPython.utils.ipstruct import Struct |
| 951 | from IPython.core.error import UsageError |
| 952 | |
| 953 | # Escape quote markers. |
nothing calls this directly
no test coverage detected