What is the difference between Caching and Memoization?

后端 未结 5 711
南方客
南方客 2021-01-30 15:17

I would like to know what the actual difference between caching and memoization is.
As I see it, both involve avoiding repeated function ca

5条回答
  •  佛祖请我去吃肉
    2021-01-30 15:53

    Memoization is a special form of caching the result of a deterministic function. This means that caching the result outside the function is not memoization because the function would have to mutate the cache when computing a new result (not already in the cache) so it would not be a (pure) function anymore. Memoization generally implies passing the cache as an additional argument (in an helper function). Memoization will optimize functions that need to compute values several times for a single access. Caching will optimize functions that are called several times with the same parameters. In other words, Memoization will optimize the first access whether caching will only optimize recurrent accesses.

提交回复
热议问题