Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.