Ask Your Question
1

How to properly time Sage computations?

asked 2018-02-27 18:08:14 +0100

anonymous user

Anonymous

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

Comments

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 19:18:27 +0100 )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):

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

print time_took
ninho gravatar imageninho ( 2018-02-27 19:26:11 +0100 )edit

That's certainly possible, see e.g. the examples using the standard python module timeon this page https://zapier.com/engineering/profil... 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 19:30:15 +0100 )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()
1519758049.43
sage: # some seconds were gone
sage: print time.time()
1519758067.58
dan_fulea gravatar imagedan_fulea ( 2018-02-27 20:05:10 +0100 )edit

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

ninho gravatar imageninho ( 2018-02-27 22:40:17 +0100 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2018-02-28 09:12:45 +0100

Sébastien gravatar image

updated 2018-02-28 09:17:00 +0100

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 misc.py:2139(factor)
        1    0.000    0.000    0.000    0.000 factorization_integer.py:30(__init__)
        2    0.000    0.000    0.000    0.000 {isinstance}
        1    0.000    0.000    0.000    0.000 proof.py:169(get_flag)
        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

Comments

What if the code has multiple lines?

Thrash gravatar imageThrash ( 2022-12-17 20:04:50 +0100 )edit
1

In a Jupyter notebook, write %%time on the first line with two %. Maybe it works also on the command line.

Sébastien gravatar imageSébastien ( 2023-05-11 11:37:49 +0100 )edit
1

answered 2018-02-28 10:21:54 +0100

tmonteil gravatar image

updated 2018-02-28 10:24:44 +0100

There is a thematic tutorial about profiling in Sage that lists various tools for timing your code, see http://doc.sagemath.org/html/en/thema...

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

Comments

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 data.prof ....
sh% gprof2dot -f pstats -o profile.dot data.prof

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 13:13:33 +0100 )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

Stats

Asked: 2018-02-27 18:08:14 +0100

Seen: 9,971 times

Last updated: Feb 28 '18