Ask Your Question

Use memory profile (python module) in SAGE

asked 2013-08-20 18:43:29 +0200

mresimulator gravatar image

updated 2013-08-20 21:58:35 +0200

Hi experts.

I want to use memory profile (decoretor module) for see a RAM memory porfile of my scipt:

in sage.

Following the web page instructions and the post

I installed doing:

 $ sudo sage --python install

When I run the scrip with @profile decorator, I obtain

 Filename: <string>

 ERROR: Could not find file <string>

What am I doing wrong?

Please explain this to me step by step (i'm a totally newby linux and python user).

Thanks a lot!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-08-24 03:39:22 +0200

nbruin gravatar image

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:

  1. Make a file, say, with contents, say,

    from memory_profiler import profile
    def my_func():
         a = [1] * (10 ** 6)
         b = [2] * (2 * 10 ** 7)
         del b
         return a
  2. Now you can do (make sure that your current directory contains

sage: import test 
sage: _ = test.my_func()

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 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 (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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-08-20 18:43:29 +0200

Seen: 1,694 times

Last updated: Aug 24 '13