Does a computer remember results of evaluated expressions?

70 Views Asked by At

Does a computer store results of evaluated expressions to speed up future evaluations during the same execution session of a program, or does the program need to include this functionality manually?

1

There are 1 best solutions below

1
On BEST ANSWER

No, computers do not do this automatically. They do almost always have caches, which store copies of data that has recently been loaded from (or stored to) memory, since storage/retrieval from the cache is orders of magnitude faster than from main memory. However, the cache is not used to store the intermediate results of expressions.

You will have to write code that explicitly stores these values. This is a common high-level optimization technique for certain algorithms, known as memoization. The canonical example is using it with a recursive algorithm to calculate factorials. Doing it the naive way, you'll end up calculating the low-order factorials an exponential number of times, which is extremely slow. However, if you save these values (e.g., in an array, where they can be looked up by index in constant time), you will speed up the algorithm significantly. But the computer won't do this automatically. It has no way of knowing which expressions' results you want to save, and always doing this would be a pessimization, rather than an optimization.