Ask Your Question

How to properly time Sage computations?

asked 2018-02-27 11:08:14 -0500

anonymous user


I have written a Sage code that performs some calculations with elliptic curves, and I want to get how much time it took to finish the calculations. I saw that there is already a function called timeit, but what I see is that it basically times the given command. Instead what I need is that I want to time the execution of certain code block (for example, a loop). Assuming, I have a code segment as this:

for i in range(0, 100):
     # Do some calculations...

How can I get how much time it took to execute the code block (in this case the whole loop)?

edit retag flag offensive close merge delete


I'm not sure if I understand what you want but you could try defining a function which runs your code block and then use timeit on that.

j.c. gravatar imagej.c. ( 2018-02-27 12:18:27 -0500 )edit

@j.c. What I want to achieve it something like this (I do not want to pass functions to timeit, instead I want to define start and end of a block that I want to time):

for i in range(0, 100):
     # Do some calculations..

print time_took
ninho gravatar imageninho ( 2018-02-27 12:26:11 -0500 )edit

That's certainly possible, see e.g. the examples using the standard python module timeon this page but that may not be as accurate as using timeit since various background processes could affect the result.

j.c. gravatar imagej.c. ( 2018-02-27 12:30:15 -0500 )edit

A good idea is to use a profiler. profile This will measure "all time consuming places" in the given code.

An other (not so good, but very often effective and cheap) idea is to print the second when the code passes through some points, then to manually look for the corresponding lines. E.g:

sage: import time
sage: print time.time()
sage: # some seconds were gone
sage: print time.time()
dan_fulea gravatar imagedan_fulea ( 2018-02-27 13:05:10 -0500 )edit

@j.c.@dan_fulea Thanks for the tips.

ninho gravatar imageninho ( 2018-02-27 15:40:17 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2018-02-28 02:12:45 -0500

Sébastien gravatar image

updated 2018-02-28 02:17:00 -0500

Let me write here the 3 most basic way to "properly time Sage computations". I think before looking at more intricate way to time code (e.g. asking to modify the code itself), one must first start with the basic ones or at least know they exists so that easy methods are used when they are sufficient.

Timing one call to a function with %time :

sage: %time factor(2^300-1)
CPU times: user 5.42 ms, sys: 162 µs, total: 5.58 ms
Wall time: 7.31 ms
3^2 * 5^3 * 7 * 11 * 13 * 31 * 41 * 61 * 101 * 151 * 251 * 331 * 601 * 1201 * 1321 * 1801 * 4051 * 8101 * 63901 * 100801 * 268501 * 10567201 * 13334701 * 1182468601 * 1133836730401

Doing lots of call to a function and returns the best of 3 with %timeit:

sage: %timeit factor(2^300-1)
100 loops, best of 3: 5.72 ms per loop

Profiling python code with %prun:

sage: %prun factor(2^300-1)
         8 function calls in 0.007 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.007    0.007    0.007    0.007 {method 'factor' of 'sage.rings.integer.Integer' objects}
        1    0.000    0.000    0.007    0.007 <string>:1(<module>)
        1    0.000    0.000    0.007    0.007
        1    0.000    0.000    0.000    0.000
        2    0.000    0.000    0.000    0.000 {isinstance}
        1    0.000    0.000    0.000    0.000
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Most of the time, I am able to reduce my timing problem to using one of these three.

edit flag offensive delete link more

answered 2018-02-28 03:21:54 -0500

tmonteil gravatar image

updated 2018-02-28 03:24:44 -0500

There is a thematic tutorial about profiling in Sage that lists various tools for timing your code, see

Note that runsnakerun will display (graphically) the time spent in each function, so that you can quickly see where the bottelnecks are.

edit flag offensive delete link more


I have actually found that the following gives me a profile summary that is a little bit easier to read than runsnakerun (although the interactive features of the latter can be very useful to explore profile data as well):

sage: %prun -D ....
sh% gprof2dot -f pstats -o

after which you need a viewer that is capable of viewing dot files (such as xdot) or software to convert a dot file to an image format such as pdf (which "dot" will do).

It displays your profile data as a call graph with colors and arrow weights to indicate relative costs.

nbruin gravatar imagenbruin ( 2018-02-28 06:13:33 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2018-02-27 11:08:14 -0500

Seen: 67 times

Last updated: Feb 28

Related questions