Inner loop

Inner loop

In computer programs, an important form of control flow is the "loop". For example, this small pseudo-code program uses two nested loops to iterate over all the entries of an "n"×"n" matrix, changing their values so that the matrix becomes an identity matrix:

for a in 1..n for b in 1..n if a = b M [a,b] := 1 else M [a,b] := 0

However, note that the comparison a = b is made n^2 times, which is a lot if n is large. We can do better:

for a in 1..n for b in 1..n M [a,b] := 0 M [a,a] := 1

At a first glance, one might think that the second variant of the algorithm is slower than the first, since it changes the value of some of the entries twice. But the number of extra changes is only n, and the number of comparisons that don't have to be done is n^2; clearly, for large enough values of n, the second algorithm will be faster no matter the relative cost of comparisons and assignments, since we do less work in the innermost loop.

Here's a second example:

for a in 1..10000 do_something_A for b in 1..10000 do_something_B

Assume that do_something_A takes 100 μs to run, and do_something_B takes 1 μs. The entire program then takes 10000*100 μs + 10000^2*1 μs = 101 s. We will spend one day optimizing this program, and during that day we can either make do_something_A 50 times faster, or do_something_B 10% faster. Which should we choose? Well, the first option will bring down the total execution time to 10000*2 μs + 10000^2*1 μs = 100.02 s, and the second option will make it 10000*100 μs + 10000^2*0.9 μs = 91 s – clearly, optimizing the innermost loop is the better choice. But what if we could make do_something_A 500 times faster? Or 5000? The answer is still the same, because of those initial 101 seconds, 100 seconds are spent in do_something_B, and just one second in do_something_A. Even if we could make do_something_A take no time at all, making do_something_B 10% faster would still be the better choice!

So: since almost all the program's time is spent in the innermost loops, optimizations there will have a big effect on the total time it takes to run the program. In contrast, optimizing anything "but" the innermost loops is often a waste of the programmer's time since it speeds up a part of the program that never did take much time.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • inner loop — vidinis kontūras statusas T sritis automatika atitikmenys: angl. inner loop vok. innere Schleife, f; innerer Kreis, m rus. внутренний контур, m pranc. boucle intérieure, f; boucle secondaire, f …   Automatikos terminų žodynas

  • Inner Loop (Rochester) — Inner Loop Map of Rochester with the Inner Loop highlighted in red …   Wikipedia

  • Inner loop (disambiguation) — Inner loop can refer to: *The central area of Houston, TX, U.S.A. contained almost entirely within the Loop. *An inner loop in computer programs *A label for clockwise traveling lanes of a roadway *The inner loop (clockwise roadway) of Interstate …   Wikipedia

  • Inner Loop (Washington, D.C.) — Infobox road marker state=DC highway name=Inner Loop alternate name= maint= length mi= length round= length ref= length notes= established=1971 decommissioned=1977 direction a=West terminus a=14th Street Bridge at the Pentagon direction b=East… …   Wikipedia

  • Loop nest optimization — (LNO) is a special case of loop transformation, dealing with nested loops, that allows large reductions in the cache bandwidth necessary for some common algorithms.Example: Matrix multiplyMany large mathematical operations on computers end up… …   Wikipedia

  • Inner/outer directions — In nations that drive on the right, traffic flows clockwise in the inner lanes (blue) of a beltway and counterclockwise in the outer lanes (red) …   Wikipedia

  • Loop optimization — In compiler theory, loop optimization plays an important role in improving cache performance, making effective use of parallel processing capabilities, and reducing overheads associated with executing loops. Most execution time of a scientific… …   Wikipedia

  • Loop — A loop is generally something that closes back on itself such as a circle. The closing can appear in time or in space.cience and technology*Loop (algebra), a quasigroup with an identity element *Loop (graph theory), an edge that begins and ends… …   Wikipedia

  • Loop interchange — In compiler theory, loop interchange is the process of exchanging the order of two iteration variables. For example, in the code fragment: for i from 0 to 10 for j from 0 to 20 a [i,j] = i + jloop interchange would result in: for j from 0 to 20… …   Wikipedia

  • Loop counter — In software engineering, a loop counter is the term often used to refer to the variable that controls the iterations of a loop (a computer programming language construct). It is so named because most uses of this construct result in the variable… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”