0

Hello,

Pls can somebody help me with time comparison between 2 scripts. I have already looked the documentation but I haven't found anything.

I deal with following exercise:
"Run this version of fibonacci and the original with a range of parameters and
compare their run times."

I intend to measure the time for this script.

import time
known = {0:0, 1:1}
def fibonacci(n):
        t0=time.time()
        if n in known:
            return known[n]
        res = fibonacci(n-1) + fibonacci(n-2)
       
        known[n] = res
        print t0
        return res

print fibonacci(3)

Thank you for help

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by vlady
0

You need to put global known in your function.

put t0=time.clock() before
and after print time.clock()-t0 to see how long it took. But put the timing in main function as you need total not time for each call of fibonacci.

Nice example of lookup function.

Edited by pyTony: ICODE

0

You need to put global known in your function.

put t0=time.clock() before
and after print time.clock()-t0 to see how long it took. But put the timing in main function as you need total not time for each call of fibonacci.

Nice example of lookup function.

Thank you , it works.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.