First time here? Check out the FAQ!

Ask Your Question
0

time in notebook

asked 14 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 14 years ago

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.

Preview: (hide)
link
0

answered 14 years ago

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

Preview: (hide)
link

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 ( 14 years ago )

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: 14 years ago

Seen: 556 times

Last updated: Dec 08 '10