Open In App

Why does a nested loop perform much faster than the flattened one?

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.  

In this article, we will see why does a nested loop performs better than the flattened one. But first, let’s see what is a nested loop and what is a flattened loop.4

A nested loop performs faster than a flattened one because it takes advantage of spatial locality, which is the principle that data that is close to each other in memory is also likely to be close to each other in time.

When a nested loop iterates through an array, it typically accesses elements that are close to each other in memory. This allows the CPU to access data from memory more quickly because it can take advantage of the CPU cache, which is a small amount of memory that is built into the CPU itself. This can greatly reduce the number of times that the CPU has to access main memory, which is much slower than the CPU cache.

On the other hand, a flattened loop iterates through the entire array, which may cause the CPU to frequently access memory that is not currently in the cache. This increases the number of times the CPU has to access main memory, which can slow down the loop considerably.

Additionally, In some architectures, accessing memory in a sequential manner allows to use prefetching mechanism, that can predict next memory location and fetch it to CPU cache before it’s actually needed, which can also improve performance.

  • Nested loops are the logical structure in computer programming and coding. In which one loop statement is inside another loop statement.

Syntax:

for [iter_1] in [sequence_1]:
    for [iter_2] in [sequence_2]:
          
          # statements of inner loop
          statements(iter_2)

    # statements of outer loop
    statements(iter_1)  
  • Loops have the ability to iterate over the items of any sequence, such as a list or a string.

Syntax:

for [iter] in sequence:
     statements(s)

When we run our python script, the operating system we are running on will assign a Process ID for it. It can be interrupted by system calls and its priority can be changed over time. But the system is not likely to take resources away from a process when we change memory address or values. When we run flat for loop it is assigning much fewer variables than a nested loop. So we can say that a nested loop utilizes resources more than a flat loop if they are available. 

Example:

Python3




# import module
import time
 
# flattened loop
def loop(n):
    for i in range(n**3):
        pass
 
 
# nested loop
def nested(n):
   
    for i in range(n):
        for j in range(n):
            for k in range(n):
                pass
 
for i in range(10, 100, 10):
    start = time.time()
    loop(i)
    print('For flattened loop:',time.time() - start)
     
    start = time.time()
    nested(i)
    print('For nested loop:',time.time() - start)
    print()


Output:

For flattened loop: 2.7894973754882812e-05
For nested loop: 4.9114227294921875e-05

For flattened loop: 0.0002155303955078125
For nested loop: 0.00024271011352539062

For flattened loop: 0.0007171630859375
For nested loop: 0.0007529258728027344

For flattened loop: 0.0016894340515136719
For nested loop: 0.0012614727020263672

For flattened loop: 0.0029077529907226562
For nested loop: 0.0022766590118408203

For flattened loop: 0.004510402679443359
For nested loop: 0.003597736358642578

For flattened loop: 0.007539272308349609
For nested loop: 0.0057599544525146484

For flattened loop: 0.01167440414428711
For nested loop: 0.008468151092529297

For flattened loop: 0.016645431518554688
For nested loop: 0.01381683349609375

Advantages and Disadvantages:

Advantages of nested loops:

  • They take advantage of spatial locality, which can greatly improve performance by reducing the number of times the CPU has to access main memory.
  • They can be used to iterate over multi-dimensional arrays, which can make the code more readable and easier to understand.
  • They allow for better data encapsulation and organization, as the different levels of the loop can represent different aspects of the data.

Disadvantages of nested loops:

  • They can be more difficult to understand and debug than flattened loops, especially when there are multiple levels of nesting.
  • They can be less efficient than flattened loops when the data is not organized in a multi-dimensional structure.
  • They can consume more memory if the nested data structure is large, as it can take more memory to store the nested elements.
  • It’s worth noting that in some cases, nested loops can be flattened using techniques like linearizing, or using more advanced data structures like sparse
  • matrices to represent the data. It’s important to have a good understanding of the problem and the data structure to choose the best approach, and it’s also
  • important to measure the performance of different solutions to select the best one.

Reference :

  • There are several books that cover the topic of nested loops and their performance in depth. Some popular books on this subject include:
  • “Introduction to Algorithms” by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein: This is a widely used textbook that provides a comprehensive introduction to algorithms, including a discussion of nested loops and their performance.
  • “Programming Pearls” by Jon Bentley: This book is a classic collection of essays on programming techniques, including a chapter on nested loops and their performance.
  • “Computer Science: An Overview” by J. Glenn Brookshear: This is a comprehensive introduction to computer science that covers various topics including algorithms, data structures, and programming concepts.
  • “Algorithms in a Nutshell” by George T. Heineman, Gary Pollice, and Stanley Selkow: This book provides a quick reference for various algorithms, including those related to nested loops and their performance.
  • These are just a few examples, and there are many other resources available that can help you understand the performance of nested loops and other algorithmic concepts.

We can see that the time taken by the nested loop decreases for the increasing value of n.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads