Question

While reading another interesting post regarding for loop optimization, I noticed one of the commentors suggested using memoization technique.

I dug around a bit to learn more and found several examples of how to memoize functions such as factorial or fibonacci sequence just to name two.

I can follow these and understand the general idea but I'm struggling with how this can be applied to a simple arithmetic operation such as the one in the original post:

for (j=0; j < ARRAY_SIZE; j++) {
    sum += array[j];
}

What would be the portion that has already been calculated?

Is it possible or was the commentor incorrect on their assumption that it could apply here?


Just interested in learning memoization techniques in this post, I know there are other methods such as unrolling, that provide significant optimization.

Was it helpful?

Solution

That for-loop can't be memoized in itself. Memoization is applicable when you can store previously solved subproblems and use them to calculate bigger subproblems/problems. It's therefore closely related to functions that can be solved by recursion since this also means that a problem is split into subproblems. (See also: Dynamic programming).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top