Measure memory usage of a Python statement Usage, in line mode: %memit [-r t i ] statement Usage, in cell mode: %%memit [-r t i ] setup_code code... code... This function can be used both as a line and cell magic:
(self, line='', cell=None)
| 1027 | # a timeit-style %memit magic for IPython |
| 1028 | @line_cell_magic |
| 1029 | def memit(self, line='', cell=None): |
| 1030 | """Measure memory usage of a Python statement |
| 1031 | |
| 1032 | Usage, in line mode: |
| 1033 | %memit [-r<R>t<T>i<I>] statement |
| 1034 | |
| 1035 | Usage, in cell mode: |
| 1036 | %%memit [-r<R>t<T>i<I>] setup_code |
| 1037 | code... |
| 1038 | code... |
| 1039 | |
| 1040 | This function can be used both as a line and cell magic: |
| 1041 | |
| 1042 | - In line mode you can measure a single-line statement (though multiple |
| 1043 | ones can be chained with using semicolons). |
| 1044 | |
| 1045 | - In cell mode, the statement in the first line is used as setup code |
| 1046 | (executed but not measured) and the body of the cell is measured. |
| 1047 | The cell body has access to any variables created in the setup code. |
| 1048 | |
| 1049 | Options: |
| 1050 | -r<R>: repeat the loop iteration <R> times and take the best result. |
| 1051 | Default: 1 |
| 1052 | |
| 1053 | -t<T>: timeout after <T> seconds. Default: None |
| 1054 | |
| 1055 | -i<I>: Get time information at an interval of I times per second. |
| 1056 | Defaults to 0.1 so that there is ten measurements per second. |
| 1057 | |
| 1058 | -c: If present, add the memory usage of any children process to the report. |
| 1059 | |
| 1060 | -o: If present, return a object containing memit run details |
| 1061 | |
| 1062 | -q: If present, be quiet and do not output a result. |
| 1063 | |
| 1064 | Examples |
| 1065 | -------- |
| 1066 | :: |
| 1067 | |
| 1068 | In [1]: %memit range(10000) |
| 1069 | peak memory: 21.42 MiB, increment: 0.41 MiB |
| 1070 | |
| 1071 | In [2]: %memit range(1000000) |
| 1072 | peak memory: 52.10 MiB, increment: 31.08 MiB |
| 1073 | |
| 1074 | In [3]: %%memit l=range(1000000) |
| 1075 | ...: len(l) |
| 1076 | ...: |
| 1077 | peak memory: 52.14 MiB, increment: 0.08 MiB |
| 1078 | |
| 1079 | """ |
| 1080 | from memory_profiler import memory_usage, _func_exec |
| 1081 | opts, stmt = self.parse_options(line, 'r:t:i:coq', posix=False, |
| 1082 | strict=False) |
| 1083 | |
| 1084 | if cell is None: |
| 1085 | setup = 'pass' |
| 1086 | else: |
nothing calls this directly
no test coverage detected