In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached results when required.

The most common example of memoization is the implementation of a fibonacci series.

Because a fibonacci series recursively calls upon itself, when calculating the series for any larger numbers, you end up making the same calculations over and over again.

With memoization you store the results of certain calculations along the way, and then simply lookup and return them when called again in the future.

For example, here is a naive implementation of a fibonacci series. One that does the full on fibonacci calculation for every element beginning to end.

### FibonacciNaive.java

```public class FibonacciNaive {

public int fib(int n) {
System.out.println("n = " + n);
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
```

### FibonacciNaive.java

```public class FibonacciNaive {

public int fib(int n) {
System.out.println("n = " + n);
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
```

This is an extremely slow implementation. fib(30) here takes about 19 seconds.

By contrast, here is the say series, only memoized. Here we store the result of fib(n) as we calculate it, and then use it each time in subsequent calculations.

### FibonacciMemoized.java

```public class FibonacciMemoized {

private int[] memo = new int[1001];

public int fib(int n) {
System.out.println("n = " + n);
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} else if (memo[n] == 0){
memo[n] = fib(n - 1) + fib(n - 2);
}
return memo[n];
}
}

```

This one does fib(30) is less than a second. You have to bump it up to 1000 to approx 20s. So a massive increase in computational performance.

That’s the power and beauty of memoization. Also our runtime is now linear with memoization instead of exponential.

So that’s the power of memoization. Happy coding!

Here is the test harness.

### FibonacciTest.java

```import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;

public class FibonacciTest {

private FibonacciNaive naive;
private FibonacciMemoized memo;

@Before
public void setUp() throws Exception {
naive = new FibonacciNaive();
memo = new FibonacciMemoized();
}

@Test
public void Naive() throws Exception {
Assert.assertEquals(0, naive.fib(0));
Assert.assertEquals(1, naive.fib(1));
Assert.assertEquals(1, naive.fib(2));
Assert.assertEquals(2, naive.fib(3));
Assert.assertEquals(3, naive.fib(4));
Assert.assertEquals(5, naive.fib(5));
Assert.assertEquals(8, naive.fib(6));
Assert.assertEquals(13, naive.fib(7));
Assert.assertEquals(21, naive.fib(8));
}

@Test
public void Memoized() throws Exception {
Assert.assertEquals(0, memo.fib(0));
Assert.assertEquals(1, memo.fib(1));
Assert.assertEquals(1, memo.fib(2));
Assert.assertEquals(2, memo.fib(3));
Assert.assertEquals(3, memo.fib(4));
Assert.assertEquals(5, memo.fib(5));
Assert.assertEquals(8, memo.fib(6));
Assert.assertEquals(13, memo.fib(7));
Assert.assertEquals(21, memo.fib(8));
}

@Test
public void RecordTimeNaive() throws Exception {
long startTime = System.currentTimeMillis();
naive.fib(30);
long endTime = System.currentTimeMillis();
long elapsedTime = (endTime - startTime) / 1000;
System.out.println("elapsedTime = " + elapsedTime); // 19s
}

@Test
public void RecordTimeMemoized() throws Exception {
long startTime = System.currentTimeMillis();
memo.fib(1000);
long endTime = System.currentTimeMillis();
long elapsedTime = (endTime - startTime) / 1000;
System.out.println("elapsedTime = " + elapsedTime); // 20s
}
}

```