Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
A nested loop puzzle?
In this section we will see one interesting nested loop performance puzzle. We will analyze two code segments with different loop arrangements to understand which one runs faster, assuming the compiler does not optimize the code.
The Problem
Both code segments execute the same total number of iterations (10 × 100 = 1000), but their performance characteristics differ due to loop overhead −
Segment 1: Outer Loop with Fewer Iterations
#include <stdio.h>
int main() {
int count = 0;
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 100; j++) {
count++; // Some work
}
}
printf("Total iterations: %d
", count);
return 0;
}
Total iterations: 1000
Segment 2: Outer Loop with More Iterations
#include <stdio.h>
int main() {
int count = 0;
for(int i = 0; i < 100; i++) {
for(int j = 0; j < 10; j++) {
count++; // Same work
}
}
printf("Total iterations: %d
", count);
return 0;
}
Total iterations: 1000
Performance Analysis
Although both segments execute 1000 iterations, Segment 1 is faster due to reduced loop overhead −
| Aspect | Segment 1 | Segment 2 |
|---|---|---|
| Inner loop executions | 10 times | 100 times |
| Loop initialization overhead | 10 times | 100 times |
| Condition checking overhead | 110 times (10×11) | 1100 times (100×11) |
| Performance | Faster | Slower |
Key Insight
The inner loop has overhead costs for initialization, condition checking, and increment operations. In Segment 1, the inner loop initializes only 10 times, while in Segment 2, it initializes 100 times. This makes Segment 1 more efficient despite identical total iterations.
Conclusion
When designing nested loops, place the loop with fewer iterations on the outside to minimize loop overhead. This optimization can provide measurable performance improvements in time-critical applications.
