Using the classic Fibonacci equation we can show the differences and benefits of using a python decorator as a caching engine for functions.
Taking the below code to work out the 10th Fibonacci number, however I had added a sleep of one second every-time the Fibonacci function is called.
This increases the non-sleep execution time from 0m0.019s to 2m57.215s.
This will allow you see the changes in execution time from cached vs non-cached answers. As when the answer is cached it will skip over the sleep statement.
In this example the sleep statement is a real world place holder for timely execution time.
By adding the below function memorize, it will simply create a dictionary of past results based on the arguments used to call the function.
All you need to do to cache the answers of the function is to add @memorize decorator above the fibonacci function.
Now when you execute the code to find the 10th Fibonacci number. The function will return the cached results to speed up the answer.
Moving the execution time from 2m57.215s to 0m11.032s.
To the extreme
In the code to fibonacci(10) another 5 times, then you will really see the power of caching, as without caching this took over 10 minutes to run on my machine.
However with cache..
The above only slowed down the code by .004 of a second (for a total user time of 0m11.036s).