Ask Your Question
0

time in notebook

asked 2010-12-06 08:35:27 +0100

Hello!

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
1

answered 2010-12-06 08:51:08 +0100

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
0

answered 2010-12-08 11:20:08 +0100

v_2e gravatar image

In my case the following syntax works:

%time
for i in range(0,10):
    command1
    command2
    ...

The working example:

%time
time print("The first line took:")
time print("\nThe second line took:")
for i in range(0,10):
    sleep(1)
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

Comments

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 15:43:24 +0100 )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

Stats

Asked: 2010-12-06 08:35:27 +0100

Seen: 539 times

Last updated: Dec 08 '10