ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 28 Feb 2018 06:13:33 -0600How to properly time Sage computations?http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/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](https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html), 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)?Tue, 27 Feb 2018 11:08:14 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/Comment by ninho for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41294#post-id-41294@j.c. @dan_fulea Thanks for the tips.Tue, 27 Feb 2018 15:40:17 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41294#post-id-41294Comment by dan_fulea for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41289#post-id-41289A good idea is to use a *profiler*.
[profile](https://docs.python.org/2/library/profile.html)
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.58Tue, 27 Feb 2018 13:05:10 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41289#post-id-41289Comment by j.c. for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41288#post-id-41288That's certainly possible, see e.g. the examples using the standard python module `time` on this page https://zapier.com/engineering/profiling-python-boss/ but that may not be as accurate as using `timeit` since various background processes could affect the result.Tue, 27 Feb 2018 12:30:15 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41288#post-id-41288Comment by ninho for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41287#post-id-41287@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_tookTue, 27 Feb 2018 12:26:11 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41287#post-id-41287Comment by j.c. for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41286#post-id-41286I'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.Tue, 27 Feb 2018 12:18:27 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41286#post-id-41286Answer by Sébastien for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?answer=41302#post-id-41302Let 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.Wed, 28 Feb 2018 02:12:45 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?answer=41302#post-id-41302Answer by tmonteil for <p>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 <a href="https://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_timeit_class.html">timeit</a>, 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:</p>
<pre><code>for i in range(0, 100):
# Do some calculations...
</code></pre>
<p>How can I get how much time it took to execute the code block (in this case the whole loop)?</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?answer=41303#post-id-41303There is a thematic tutorial about profiling in Sage that lists various tools for timing your code, see http://doc.sagemath.org/html/en/thematic_tutorials/profiling.html
Note that `runsnakerun` will display (graphically) the time spent in each function, so that you can quickly see where the bottelnecks are.Wed, 28 Feb 2018 03:21:54 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?answer=41303#post-id-41303Comment by nbruin for <p>There is a thematic tutorial about profiling in Sage that lists various tools for timing your code, see <a href="http://doc.sagemath.org/html/en/thematic_tutorials/profiling.html">http://doc.sagemath.org/html/en/thema...</a></p>
<p>Note that <code>runsnakerun</code> will display (graphically) the time spent in each function, so that you can quickly see where the bottelnecks are.</p>
http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41305#post-id-41305I 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.Wed, 28 Feb 2018 06:13:33 -0600http://ask.sagemath.org/question/41284/how-to-properly-time-sage-computations/?comment=41305#post-id-41305