HOME

TheInfoList



OR:

In
computer program A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
s, an important form of
control flow In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
is the loop which causes a block of code to be executed more than once. A common idiom is to have a loop nested inside another loop, with the contained loop being commonly referred to as the inner loop.


Program optimization

Because the entire inner loop is performed for each iteration of the outer loop, optimizations of the inner loop will have much greater effect than optimizations of the outer loop. In many languages there are at least two types of loops – for loops and while loops – and they can be nested within each other. Tosin P. Adewumi has shown that performance of a while loop with an inner for loop is better than of a while loop without the inner for loop. It was observed that more computations are performed per unit time when an inner for loop is involved than otherwise. This implies, given the same number of computations to perform, the one with an inner for loop will finish faster than the one without it. This technique of
loop optimization In compiler theory, loop optimization is the process of increasing execution speed and reducing the overheads associated with loops. It plays an important role in improving cache performance and making effective use of parallel processing capa ...
was observed across several programming languages and compilers or interpreters. In some cases, a while loop with an inner while loop performed slower than a while loop without an inner loop. The two examples below, written in Python, present a while loop with an inner for loop and a while loop without an inner loop. Although both have the same terminating condition for their while loops, the first example will finish faster because of the inner for loop. The variable ''innermax'' is a fraction of the ''maxticketno'' variable in the first example. while ticket_no * innermax < max_ticket_no: for j in range(0, innermax): if (ticket_no * innermax + j)

jackpot_no: return ticket_no += 1
while ticket_no < max_ticket_no: if ticket_no

jackpot_no: return ticket_no += 1


References

4
Python Nested For Loop From Techgeekbuz
Control flow Software optimization Articles with example Python (programming language) code {{compu-prog-stub