How can calculate the time of execution of a command in sage

1,839 views
Skip to first unread message

faicel

unread,
Jun 30, 2009, 10:58:08 AM6/30/09
to sage-support
I look for the command allowing to have the time of execution of a
command in sage



Thank you in advance

Kevin Horton

unread,
Jun 30, 2009, 11:25:16 AM6/30/09
to sage-s...@googlegroups.com
On 30-Jun-09, at 10:58 , faicel wrote:

> I look for the command allowing to have the time of execution of a
> command in sage

sage: timeit?

...
Docstring:

Time execution of a command or block of commands. Displays the
best WALL TIME for execution of the given code. This is
based on
the Python timeit module, which avoids a number of common traps
for measuring execution times. It is also based on IPython's
%timeout command.

TYPICAL INPUT FORMAT:
timeit(statement, preparse=None, number=0, repeat=3,
precision=3)

EXAMPLES:
sage: timeit('2^10000')
625 loops, best of 3: ... per loop

...


--
Kevin Horton
Ottawa, Canada

William Stein

unread,
Jun 30, 2009, 11:31:05 AM6/30/09
to sage-s...@googlegroups.com
On Tue, Jun 30, 2009 at 4:58 PM, faicel<faice...@gmail.com> wrote:
>
> I look for the command  allowing to have the time  of execution of a
> command in sage
>

Also, use the "time" command, e.g.,

sage: time 2+2
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
4
sage:

william

pang

unread,
Jul 2, 2009, 7:31:59 AM7/2/09
to sage-support
Watch also for "cputime" and "walltime", which are very general and
easy to use. Sometimes "time" and "timeit" are not convenient to use
if you have more than one statement.

William Stein

unread,
Jul 2, 2009, 9:33:10 AM7/2/09
to sage-s...@googlegroups.com

One nice trick: If you're in the notebook and you put

%time

as the first line of a notebook cell, then the entire block of code is timed.

William

Gustavo Rama

unread,
Jul 14, 2009, 9:17:25 PM7/14/09
to sage-support
But how con you get the time of execution in a variable?

On Jul 2, 10:33 am, William Stein <wst...@gmail.com> wrote:

Simon King

unread,
Jul 14, 2009, 9:42:37 PM7/14/09
to sage-support
Hi Gustavo!

On 15 Jul., 03:17, Gustavo Rama <gdr...@gmail.com> wrote:
> But how con you get the time of execution in a variable?

Using cputime or walltime: No problem, since they return a number.

Using timeit:

Remember that you could obtain a timing doing
sage: timeit('f=factorial(400).factor()')
625 loops, best of 3: 955 µs per loop

In order to assign the result of the timing to a variable, do

sage: T = timeit.eval('f=factorial(400).factor()')

Then, the necessary information is contained in the attribute 'stats'
of T:

sage: T.stats
(625, 3, 3, 953.62396240234375, '\xc2\xb5s')

So, the first entry gives the number of loops, the second the number
of runs, the third I don't understand, the fourth gives the
computation time, with units given by the last entry. Anyway:

sage: print "%d loops, best of %d: %.*g %s per loop" % T.stats
625 loops, best of 3: 954 µs per loop

Note that this only gives the walltime. Note also that it is not so
nice to compare different timings: It might be that one timing
measures the time in microseconds and the other in milliseconds - IMHO
this is an oddity.

Cheers,
Simon

Gustavo Rama

unread,
Jul 15, 2009, 3:12:36 PM7/15/09
to sage-support
Thanks, I'll try it.
Cheers Gustavo

On Jul 14, 10:42 pm, Simon King <simon.k...@uni-jena.de> wrote:
> Hi Gustavo!
>
> On 15 Jul., 03:17, Gustavo Rama <gdr...@gmail.com> wrote:
>
> > But how con you get the time of execution in a variable?
>
> Using cputime or walltime: No problem, since they return a number.
>
> Using timeit:
>
> Remember that you could obtain a timing doing
>   sage: timeit('f=factorial(400).factor()')
>   625 loops, best of 3: 955 µs per loop
>
> In order to assign the result of the timing to a variable, do
>
>   sage: T = timeit.eval('f=factorial(400).factor()')
>
> Then, the necessary information is contained in the attribute 'stats'
> of T:
>
>   sage: T.stats
>   (625, 3, 3, 953.62396240234375, '\xc2\xb5s')
>
> So, the first entry gives the number of loops, the second the number
> of runs, the third I don't understand,  the fourth gives the
> computation time, with units given by the last entry. Anyway:
>
>   sage: print "%d loops, best of %d: %.*g %s per loop" % T.stats
>   625 loops, best of 3: 954 µs per loop

Gustavo Rama

unread,
Jul 15, 2009, 5:35:21 PM7/15/09
to sage-support
The third entry gives the precicion.

Robert Bradshaw

unread,
Jul 16, 2009, 4:46:51 AM7/16/09
to sage-s...@googlegroups.com
If you want to time running something just once, see cputime() and
walltime().

- Robert
Reply all
Reply to author
Forward
0 new messages