Algorithmic Efficiency - Memory

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:

    I don’t avoid pain by not remembering something; I try to remember.... Memory is empowering, and it’s what gives you your sense of continuity in the world.
    Melinda Worth Popham (b. 1944)

    The memory loaded with mere bookwork is not the thing wanted—is, in fact, rather worse than useless—in 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 (1825–95)

    Within the memory of many of my townsmen the road near which my house stands resounded with the laugh and gossip of inhabitants, and the woods which border it were notched and dotted here and there with their little gardens and dwellings, though it was then much more shut in by the forest than now.
    Henry David Thoreau (1817–1862)