Ask Your Question

time in notebook

asked 2010-12-06 01:35:27 -0500


Could you help me? I need to know the time of a sage program written in the notebook, but when I use the timeit command, it doesn't work if I use "for" iterators in the program.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2010-12-06 01:51:08 -0500

kcrisman gravatar image

Did you try %timeit as the first line in the cell? I'm not sure whether that would work.

Another option is to write a dummy program that calls your other program, like def foo(): return my_program(n) and then do timeit on foo.

edit flag offensive delete link more

answered 2010-12-08 04:20:08 -0500

v_2e gravatar image

In my case the following syntax works:

for i in range(0,10):

The working example:

time print("The first line took:")
time print("\nThe second line took:")
for i in range(0,10):
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.01 s, Wall time: 10.04 s

Hope, that helps. :)

edit flag offensive delete link more


However, this doesn't do what timeit does, which is to do it over several iterations. For instance, in the first line we really want `sage: timeit('print("The first line took:")') 625 loops, best of 3: 734 ns per loop`

kcrisman gravatar imagekcrisman ( 2010-12-08 08:43:24 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2010-12-06 01:35:27 -0500

Seen: 173 times

Last updated: Dec 08 '10