Memoization is a technique for improving performance by caching the return
values of expensive function calls. In this post I show how you can use this
technique in C#.
A typical example to illustrate the effectiveness of memoization is the
computation of the fibonacci sequence.
Output:
13532
2744
A simple way to apply the memoization as a refactoring could be the following:
Before:
After:
To use the Memoizer class and extension methods you need the code below. The
code allows one to memoize functions that have no parameters or just one
parameter. If you want to memoize functions with more parameters you could
refactor the function first to take a single Tuple as argument and than use the
memoizer on that function.