

The latter is faster because it saves one additional lookup, but only works for single-valued functions, whereas the first can be extended to also pass along multiple arguments.

""" Memoization decorator for a function taking a single argument """ Regarding the memoization, here are two different ways to do this (for single-valued functions): import functools Running memo_factorial 20000 times, with n from 10 to 200Īll remarks are welcome, thank you very much! # check_correctness() # comment/uncomment to check or notįor func in :īenchmarking on my computer : Running naive_factorial 20000 times, with n from 10 to 200 Print('Duration : %fs' % (ending_time - starting_time)) % (func._name_, nb_runs, min_val, max_val)) Print('Running %s %d times, with n from %d to %d' Print('Error in functions implementation')ĭef multi_run_chrono(func, nb_runs, min_val, max_val): Print('Checking both functions do the same things') If not hasattr(memo_factorial, 'lookup_table'): """calculates n! using memoization in a recursive algorithm""" """calculates n! with a simple recursive algorithm""" Here is the code: from _future_ import print_function Are there some nice tricks I could do to improve this code's elegance and/or performance?.except KeyError or rather do if key in dictionary.? How can I modify multi_run_chrono to use it like Python's decorators (with Should I keep the try.Does it make a difference if I used a class attribute instead of a function attribute for my lookup_table?.I wrote a Python module to learn about memoization, and I have some questions on what I did.
