r/ProgrammerHumor • • 1d ago

Meme dynamicProgramming

Post image
5.3k Upvotes

140 comments sorted by

View all comments

Show parent comment

18

u/bwwatr 1d ago

Now that's a CS word that always baffled me. Like, it's caching. That's the whole thing.

23

u/elingeniero 1d ago

It is, but memoisation has the specific meaning of caching the result of an (expensive) pure function, like factorisation. To me the difference is that a memoised result can never be wrong and you only have to consider the memory impact, whereas caching implies that the result may become out of date if you hold it for too long, e.g. get the comments on this reddit post.

1

u/guyblade 13h ago

My previous job had a very nice memoization decorator that you could apply to arbitrary functions in python. Of course, it evolved to be a generic caching mechanism and had settings for (1) expiring memoized results after a timeout, (2) memoizing based on only some of the function inputs (e.g., memoize based on the parameters and not the database handle that you passed in), (3) limiting the total size of memoized objects, &c.

I guess nothing can stay pure.

1

u/elingeniero 10h ago

lru_cache ?

2

u/SpiritedEclair 1d ago

Caching has side effects!

A pure function can be thought of as looking at a potentially infinitely wide associative array.

1

u/janyk 1d ago

What side effects?

3

u/SpiritedEclair 1d ago

Invalidation of another entry, updating counters, in a distr system eg for CDN it means transferring data to the CDN, and potentially needing to invalidate it.