Memory
Often, it is possible to make an algorithm faster at the expense of memory. This might be the case whenever the result of an 'expensive' calculation is cached rather than recalculating it afresh each time. The additional memory requirement would, in this case, be considered additional overhead although, in many situations, the stored result occupies very little extra space and can often be held in pre-compiled static storage, reducing not just processing time but also allocation and deallocation of working memory. This is a very common method of improving speed, so much so that some programming languages often add special features to support it, such as C++'s 'mutable' keyword.
The memory requirement of an algorithm is actually two separate but related things:-
- The memory taken up by the compiled executable code (the object code or binary file) itself (on disk or equivalent, depending on the hardware and language). This can often be reduced by preferring run-time decision making mechanisms (such as virtual functions and run-time type information) over certain compile-time decision making mechanisms (such as macro substitution and templates). This, however, comes at the cost of speed.
- Amount of temporary "dynamic memory" allocated during processing. For example, dynamically pre-caching results, as mentioned earlier, improves speed at the cost of this attribute. Even the depth of sub-routine calls can impact heavily on this cost and increase path length too, especially if there are 'heavy' dynamic memory requirements for the particular functions invoked. The use of copied function parameters (rather than simply using pointers to earlier, already defined, and sometimes static values) actually doubles the memory requirement for this particular memory metric (as well as carrying its own processing overhead for the copying itself. This can be particularly relevant for quite 'lengthy' parameters such as html script, JavaScript source programs or extensive freeform text such as letters or emails.
(See also sections Choice of instruction or data type and Avoiding costs concerning deliberate 'stretching' of data structures to force them to have a fixed length, which then becomes a multiple of 2, 4, 8 etc.)
Read more about this topic: Algorithmic Efficiency
Famous quotes containing the word memory:
“It must be a peace without victory.... Victory would mean peace forced upon the losers, a victors terms imposed upon the vanquished. It would be accepted in humiliation, under duress, at an intolerable sacrifice, and would leave a sting, a resentment, a bitter memory upon which the terms of peace would rest, not permanently, but only as upon quicksand.”
—Woodrow Wilson (18561924)
“In excited conversation we have glimpses of the universe, hints of power native to the soul, far-darting lights and shadows of an Andes landscape, such as we can hardly attain in lone meditation. Here are oracles sometimes profusely given, to which the memory goes back in barren hours.”
—Ralph Waldo Emerson (18031882)
“Twenty or thirty years ago, in the army, we had a lot of obscure adventures, and years later we tell them at parties, and suddenly we realize that those two very difficult years of our lives have become lumped together into a few episodes that have lodged in our memory in a standardized form, and are always told in a standardized way, in the same words. But in fact that lump of memories has nothing whatsoever to do with our experience of those two years in the army and what it has made of us.”
—Václav Havel (b. 1936)