pythonsagememory-profilingipython-magic

Is there a way to run `%memit` inside a script without wrapping it in iphyton


I am logging the RAM usage of sagemath commands against each other and want to automate it

I found %memit and I found information on wrapping each [command][How to run an IPython magic from a script (or timing a Python script) but that is not ideal

from sage.all_cmdline import *   # import sage library
from sage.misc.sage_timeit import sage_timeit

from IPython import get_ipython
ipython = get_ipython()

if '__IPYTHON__' in globals():
            ipython.magic('load_ext autoreload')
            ipython.magic('load_ext memory_profiler')

ipython.magic("TIME=%timeit -o print 'test'")
ipython.magic("TIME=%timeit -o print 'test'")
AttributeError: 'NoneType' object has no attribute 'magic'

Is there a better way to use memit?


Solution

  • Check the memory-profiler manual: https://pypi.org/project/memory-profiler/

    For instance, it supplies a decorator for profiling functions, which might suit your needs:

    from memory_profiler import profile
    
    @profile
    def my_func():
        a = [1] * (10 ** 6)
        b = [2] * (2 * 10 ** 7)
        del b
        return a
    
    my_func()
    

    You could also use the command line interface:

    python -m memory_profiler example.py

    The manual describes several other ways of invoking it...