def magic_memit(self, line=''):
    """Measure memory usage of a Python statement

    Usage, in line mode:
      %memit [-r<R>t<T>i<I>] statement

    Options:
    -r<R>: repeat the loop iteration <R> times and take the best result.
    Default: 1

    -t<T>: timeout after <T> seconds. Default: None

    -i<I>: Get time information at an interval of I times per second.
        Defaults to 0.1 so that there is ten measurements per second.

    -c: If present, add the memory usage of any children process to the report.

    Examples
    --------
    ::

      In [1]: import numpy as np

      In [2]: %memit np.zeros(1e7)
      maximum of 1: 76.402344 MiB per loop

      In [3]: %memit np.ones(1e6)
      maximum of 1: 7.820312 MiB per loop

      In [4]: %memit -r 10 np.empty(1e8)
      maximum of 10: 0.101562 MiB per loop

    """
    opts, stmt = self.parse_options(line, 'r:t:i:c', posix=False, strict=False)
    repeat = int(getattr(opts, 'r', 1))
    if repeat < 1:
        repeat == 1
    timeout = int(getattr(opts, 't', 0))
    if timeout <= 0:
        timeout = None
    interval = float(getattr(opts, 'i', 0.1))
    include_children = 'c' in opts

    # I've noticed we get less noisier measurements if we run
    # a garbage collection first
    import gc
    gc.collect()

    mem_usage = 0
    counter = 0
    baseline = memory_usage()[0]
    while counter < repeat:
        counter += 1
        tmp = memory_usage((_func_exec, (stmt, self.shell.user_ns)),
                           timeout=timeout, interval=interval, max_usage=True,
                           include_children=include_children)
        mem_usage = max(mem_usage, tmp[0])

    if mem_usage:
        print('peak memory: %.02f MiB, increment: %.02f MiB' %
              (mem_usage, mem_usage - baseline))
    else:
        print('ERROR: could not read memory usage, try with a lower interval '
              'or more iterations')
def magic_memit ( self , line = '' ) : opts , stmt = self . parse_options ( line , 'r:t:i:c' , posix = False , strict = False ) repeat = int ( getattr ( opts , 'r' , 1 ) ) if repeat < 1 : repeat == 1 timeout = int ( getattr ( opts , 't' , 0 ) ) if timeout <= 0 : timeout = None interval = float ( getattr ( opts , 'i' , 0.1 ) ) include_children = 'c' in opts import gc gc . collect ( ) mem_usage = 0 counter = 0 baseline = memory_usage ( ) [ 0 ] while counter < repeat : counter += 1 tmp = memory_usage ( ( _func_exec , ( stmt , self . shell . user_ns ) ) , timeout = timeout , interval = interval , max_usage = True , include_children = include_children ) mem_usage = max ( mem_usage , tmp [ 0 ] ) if mem_usage : print ( 'peak memory: %.02f MiB, increment: %.02f MiB' % ( mem_usage , mem_usage - baseline ) ) else : print ( 'ERROR: could not read memory usage, try with a lower interval ' 'or more iterations' )
Measure memory usage of a Python statement

    Usage, in line mode:
      %memit [-r<R>t<T>i<I>] statement

    Options:
    -r<R>: repeat the loop iteration <R> times and take the best result.
    Default: 1

    -t<T>: timeout after <T> seconds. Default: None

    -i<I>: Get time information at an interval of I times per second.
        Defaults to 0.1 so that there is ten measurements per second.

    -c: If present, add the memory usage of any children process to the report.

    Examples
    --------
    ::

      In [1]: import numpy as np

      In [2]: %memit np.zeros(1e7)
      maximum of 1: 76.402344 MiB per loop

      In [3]: %memit np.ones(1e6)
      maximum of 1: 7.820312 MiB per loop

      In [4]: %memit -r 10 np.empty(1e8)
      maximum of 10: 0.101562 MiB per loop