Ask Your Question
3

%time inline in notebook?

asked 2010-08-23 14:13:36 +0200

ccanonc gravatar image

updated 2011-04-28 15:45:52 +0200

Kelvin Li gravatar image

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.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
8

answered 2010-08-23 14:27:14 +0200

Mike Hansen gravatar image

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.

edit flag offensive delete link more

Comments

Thanks, I knew about the %time on the first line, I was just hoping for a little magic. =)

ccanonc gravatar imageccanonc ( 2010-08-23 14:44:39 +0200 )edit
2

answered 2010-12-08 11:03:51 +0200

v_2e gravatar image

updated 2010-12-08 11:09:44 +0200

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. :)

edit flag offensive delete link more

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: 2010-08-23 14:13:36 +0200

Seen: 3,928 times

Last updated: Dec 08 '10