Here is my attempt to compare the performance of fibonacci with different ways of caching the results.
First implementation uses memoization decorator whereas the second one makes use of defaultdict for caching. You can read my previous blog post on caching with defaultdict.
Note: Default number of loops for timeit is 1000000
If you’re looking to hire a Python programmer, this would be a great programming question. Challenge all the candidates to write the fastest fibonacci function. Hire the one whose code is the fastest.
Feel free to suggest better/faster implementations if you know any, I’d be glad to add more to the collection.