Ask Your Question

Revision history [back]

You could include an optional argument which clears the function's cache:

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

You could modify this to only remove certain inputs from the cache (just remove certain keys from fibo.cache).

Also, you can use %time instead of timeit, or tell timeit to just run once:

sage: timeit('fibo(12, True)',number=1, repeat=1)
1 loops, best of 1: 4.69 ms per loop

sage: timeit('fibo(12, False)',number=1, repeat=1)
1 loops, best of 1: 192 µs per loop

But is this really any better that @john-palmieri's suggestion?

You could include an optional argument which clears the function's cache:

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

You could modify this to only remove certain inputs from the cache (just remove certain keys from fibo.cache).

Also, you can use %time instead of timeit, or tell timeit to just run once:

sage: timeit('fibo(12, True)',number=1, repeat=1)
1 loops, best of 1: 4.69 ms per loop

sage: timeit('fibo(12, False)',number=1, repeat=1)
1 loops, best of 1: 192 µs per loop

But is this really any better that @john-palmieri's @John Palmieri's suggestion?

You could include an optional argument which clears the function's cache:

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

You could modify this to only remove certain inputs from the cache (just remove certain keys from fibo.cache).

Also, you can use %time instead of timeit, or tell timeit to just run once:

sage: timeit('fibo(12, True)',number=1, repeat=1)
1 loops, best of 1: 4.69 ms per loop

sage: timeit('fibo(12, False)',number=1, repeat=1)
1 loops, best of 1: 192 µs per loop

But is this really any better that than @John Palmieri's suggestion?