1 | initial version |
From the documentation of memory_profiler:
Note however that function my_func must be defined in a file (cannot have been defined interactively in the Python interpreter)
That is to say, you can only use the @memory_profiler.profile decorator on a routine that you have put in a .py file and then have imported, so take the following steps:
Make a file, say test.py
, with contents, say,
from memory_profiler import profile @profile def my_func(): a = [1] * (10 ** 6) b = [2] * (2 * 10 ** 7) del b return a
Now you can do (make sure that your current directory contains test.py
):
sage: import test sage: _ = test.my_func() Filename: test.py Line # Mem usage Increment Line Contents ================================================ 2 @profile 3 112.277 MB 0.000 MB def my_func(): 4 119.914 MB 7.637 MB a = [1] * (10 ** 6) 5 272.504 MB 152.590 MB b = [2] * (2 * 10 ** 7) 6 119.914 MB -152.590 MB del b 7 119.914 MB 0.000 MB return a
note that import
does not allow you to make use of the sage preprocessor, so you'd have to write python, and you might want to insert from sage.all import *
in order to make everything available in your test.py
module that would normally be available at the sage prompt.
You could write test.sage
, and do load "test.sage"
. Now there's a file test.py
(possibly with a mangled name) that contains the result of running the preprocessor on test.sage
. You could use that as a starting point to produce a file that can be used via import
.