Inner loop
This article needs additional citations for verification. (September 2018) |
In computer programs, an important form of control flow 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.[1] 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.[2]
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 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.[3]
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
- ^ Ghezzi, C; Jazayeri, M (1996). Programming Language Concepts (3rd edn.). John Wiley & Sons.
- ^ Adewumi, Tosin (August 2018). "Inner loop program construct: a faster way for program execution". Open Computer Science. 8: 115–122. doi:10.1515/comp-2018-0004.
- ^ Vishal (2021-06-09). "Nested Loops in Python". PYnative. Retrieved 2022-09-07.
4. Python Nested For Loop From Techgeekbuz
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.