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:
“The memory loaded with mere bookwork is not the thing wantedis, in fact, rather worse than uselessin the teacher of scientific subjects. It is absolutely essential that his mind should be full of knowledge and not of mere learning, and that what he knows should have been learned in the laboratory rather than in the library.”
—Thomas Henry Huxley (182595)
“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)
“Great music is that which penetrates the ear with facility and leaves the memory with difficulty. Magical music never leaves the memory.”
—Thomas Beecham (18791961)