# %time inline in notebook?

 1 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 487 ● 5 ● 18 ● 34 Kelvin Li 443 ● 10 ● 17

 7 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/ in () 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. posted Aug 23 '10 Mike Hansen 3840 ● 21 ● 46 ● 84 Thanks, I knew about the %time on the first line, I was just hoping for a little magic. =) ccanonc (Aug 23 '10)
 2 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. :) posted Dec 08 '10 v_2e 219 ● 1 ● 12 ● 30

[hide preview]