#NEWLINE# @ line_cell_magic #NEWLINE# def memit( self , line = '' , cell = None ) : #NEWLINE# #INDENT# 'Measure memory usage of a Python statement\n\n        Usage, in line mode:\n          %memit [-r<R>t<T>i<I>] statement\n\n        Usage, in cell mode:\n          %%memit [-r<R>t<T>i<I>] setup_code\n          code...\n          code...\n\n        This function can be used both as a line and cell magic:\n\n        - In line mode you can measure a single-line statement (though multiple\n          ones can be chained with using semicolons).\n\n        - In cell mode, the statement in the first line is used as setup code\n          (executed but not measured) and the body of the cell is measured.\n          The cell body has access to any variables created in the setup code.\n\n        Options:\n        -r<R>: repeat the loop iteration <R> times and take the best result.\n        Default: 1\n\n        -t<T>: timeout after <T> seconds. Default: None\n\n        -i<I>: Get time information at an interval of I times per second.\n            Defaults to 0.1 so that there is ten measurements per second.\n\n        -c: If present, add the memory usage of any children process to the report.\n\n        Examples\n        --------\n        ::\n\n          In [1]: %memit range(10000)\n          peak memory: 21.42 MiB, increment: 0.41 MiB\n\n          In [2]: %memit range(1000000)\n          peak memory: 52.10 MiB, increment: 31.08 MiB\n\n          In [3]: %%memit l=range(1000000)\n             ...: len(l)\n             ...:\n          peak memory: 52.14 MiB, increment: 0.08 MiB\n\n        ' #NEWLINE# from memory_profiler import memory_usage , _func_exec #NEWLINE# ( opts , stmt ) = self . parse_options ( line , 'r:t:i:c' , posix = False , strict = False ) #NEWLINE# if ( cell is None ) : #NEWLINE# #INDENT# setup = 'pass' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# setup = stmt #NEWLINE# stmt = cell #NEWLINE# #UNINDENT# repeat = int ( getattr ( opts , 'r' , 1 ) ) #NEWLINE# if ( repeat < 1 ) : #NEWLINE# #INDENT# ( repeat == 1 ) #NEWLINE# #UNINDENT# timeout = int ( getattr ( opts , 't' , 0 ) ) #NEWLINE# if ( timeout <= 0 ) : #NEWLINE# #INDENT# timeout = None #NEWLINE# #UNINDENT# interval = float ( getattr ( opts , 'i' , 0.1 ) ) #NEWLINE# include_children = ( 'c' in opts ) #NEWLINE# import gc #NEWLINE# gc . collect ( ) #NEWLINE# _func_exec ( setup , self . shell . user_ns ) #NEWLINE# mem_usage = 0 #NEWLINE# counter = 0 #NEWLINE# baseline = memory_usage ( ) [ 0 ] #NEWLINE# while ( counter < repeat ) : #NEWLINE# #INDENT# counter += 1 #NEWLINE# tmp = memory_usage ( ( _func_exec , ( stmt , self . shell . user_ns ) ) , timeout = timeout , interval = interval , max_usage = True , include_children = include_children ) #NEWLINE# mem_usage = max ( mem_usage , tmp [ 0 ] ) #NEWLINE# #UNINDENT# if mem_usage : #NEWLINE# #INDENT# print ( ( 'peak memory: %.02f MiB, increment: %.02f MiB' % ( mem_usage , ( mem_usage - baseline ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'ERROR: could not read memory usage, try with a lower interval or more iterations' )