Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ok, i think i didn't express my question clearly. Let try to ilustrate it with a simple example.

Imagine i want to write a function that computes the n'th term of the fibonaci series. Consider this function

@cached_function
def fibo(n):
    if n==0 or n==1:
        return 1
    return fibo(n-1)+fibo(n-2)

This function calls itself twice, without the cached decorator, fibo(4) would compute fibo(3) and fibo(2); but to compute fibo(3) it wouold compute fibo(2) and fibo(1), and again, to compute fibo(2), it would compute fibo(1) and fibo(0). That is, it would have to compute several times the same intermediate steps. With the cached decorator, this is avoided, having to compute each value between 0 and n-1 only once.

But if i want to time the cached version of the function versus the non cached version, timeit is not useful, since for the non cached version it repeats several times the same computation, and then gives an average of the time spent; as in the cached version it computes it once and then reads the cache several times, giving an unrealistic average.