%time inline in notebook?

i like this post (click again to cancel)
1
i dont like this post (click again to cancel)

I've noticed I can use %time from the terminal/sage CLI inline with a statement; but I get a syntax error in the notebook. Is there a way I can put %time inline inside a notebook cell?

"%time; statement" or "%time statement" doesn't work (in the notebook).

Aside: It would be nice if I could get the starting time (of evaluation) as a side-effect too, not just the CPU and Wall Time.

asked Aug 23 '10

ccanonc gravatar image ccanonc
477 3 15 32

updated Apr 28 '11

Kelvin Li gravatar image Kelvin Li
423 9 16

2 Answers:

i like this answer (click again to cancel)
7
i dont like this answer (click again to cancel) ccanonc has selected this answer as correct

You can't put %time statement in the middle of a notebook cell as each cell is evaluated "all together" rather than a line at a time. You can however time how long it takes to evaluated whole cells. Just put %time at the top of the cell:

%time
a = factorial(10^6)

If you really need to do some timing in the middle of the cell, there is the timeit function which takes a string as input and prints the timing information. Note that by default this will run the statement multiple times in order to get timing information. Additionally, it executes the statement in a separate namespace. For example:

sage: res = timeit('a = factorial(1000)')
625 loops, best of 3: 17.4 µs per loop
sage: a
Traceback (most recent call last)
...
/home/release/sage-4.4.4.alpha1/<ipython console> in <module>()
NameError: name 'a' is not defined

If you want to get the starting time of evaluation, you can put this near the beginning of the cell

import time
print time.localtime()

and it should give you some output like

time.struct_time(tm_year=2010, tm_mon=8, tm_mday=23, tm_hour=10, tm_min=19, tm_sec=0, tm_wday=0, tm_yday=235, tm_isdst=1)

See the Python documentation for the time module for more information about this.

link

posted Aug 23 '10

Mike Hansen gravatar image Mike Hansen flag of United States
3675 19 43 81
Thanks, I knew about the %time on the first line, I was just hoping for a little magic. =) ccanonc (Aug 23 '10)
i like this answer (click again to cancel)
2
i dont like this answer (click again to cancel)

As far as I know,

%time
command1
command2
...

works for timing the execution of the whole cell. Meanwhile, there is an inline

time

statement, which one may put at the beginning of the line like this:

time print("The first line took:")

To compare how these two tools work, you may try executing the following cell:

%time
time print("The first line took:")
time print("\nThe second line took:")
print("\nAltogether took:")

The output should look like this:

The first line took:

Time: CPU 0.00 s, Wall: 0.00 s


The second line took:

Time: CPU 0.00 s, Wall: 0.00 s


Altogether took:

CPU time: 0.00 s, Wall time: 0.00 s

Hope that helps. :)

link

posted Dec 08 '10

v_2e gravatar image v_2e flag of Ukraine
219 1 11 24

updated Dec 08 '10

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

Tags:

Stats:

Asked: Aug 23 '10

Seen: 204 times

Last updated: Dec 08 '10

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.