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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.