def memoized_version_of_f(*args): """Wrapper using memoization """ res = simcache.get_key(args) if not res: res = decorated_func(*args) # Call the real function simcache.set_key(args, res) return res
def memoized_version_of_func(n): """Wrapper using memoization """ res = simcache.get_key(n) if not res: res = func_to_memoize(n) # Call the real function simcache.set_key(n, res) return res
def factorial(x): time.sleep(0.1) # This sleep can not be removed!! if x < 2: return 1 res = simcache.get_key(x - 1) if not res: res = factorial(x - 1) simcache.set_key(x - 1, res) return x * res
def memoized_fibonacci(n): # Try to retrieve value from cache res = simcache.get_key(n) if not res: # If failed, call real fibonacci func print "Memoized fibonacci func, proceeding to call real func", real_fibonacci, n res = real_fibonacci(n) # Store real result simcache.set_key(n, res) return res
def fibonacci(n): res = simcache.get_key(n) if not res: res = real_fibonacci(n) simcache.set_key(n, res) return res