Which of the following two code segments is faster? Assume that compiler makes no optimizations.
/* FIRST */for(i=0;i<10;i++) for(j=0;j<100;j++) //do something |
/* SECOND */for(i=0;i<100;i++) for(j=0;j<10;j++) //do something |
Both code segments provide same functionality, and the code inside the two for loops would be executed same number of times in both code segments.
If we take a closer look then we can see that the SECOND does more operations than the FIRST. It executes all three parts (assignment, comparison and increment) of the for loop more times than the corresponding parts of FIRST:
- The SECOND executes assignment operations ( j = 0 or i = 0) 101 times while FIRST executes only 11 times.
- The SECOND does 101 + 1100 comparisons (i < 100 or j < 10) while the FIRST does 11 + 1010 comparisons (i < 10 or j < 100).
- The SECOND executes 1100 increment operations (i++ or j++) while the FIRST executes 1010 increment operation.
Below C++ code counts the number of increment operations executed in FIRST and SECOND, and prints the counts.
//program to count number of increment //operations in FIRST and SECOND #include<iostream> using namespace std; int main() { int c1 = 0, c2 = 0; /* FIRST */ for(int i=0;i<10;i++,c1++) for(int j=0;j<100;j++, c1++); //do something /* SECOND */ for(int i=0; i<100; i++, c2++) for(int j=0; j<10; j++, c2++); //do something cout << " Count in FIRST = " <<c1 << endl; cout << " Count in SECOND = " <<c2 << endl; getchar(); return 0; } |
Count in FIRST = 1010 Count in SECOND = 1100
Below C++ code counts the number of comparison operations executed by FIRST and SECOND
//program to count the number of comparison //operations executed by FIRST and SECOND */ #include<iostream> using namespace std; int main() { int c1 = 0, c2 = 0; /* FIRST */ for(int i=0; ++c1&&i;<10; i++) for(int j=0; ++c1&&j;<100;j++); //do something /* SECOND */ for(int i=0; ++c2&&i;<100; i++) for(int j=0; ++c2&&j;<10; j++); //do something cout << " Count fot FIRST " <<c1 << endl; cout << " Count fot SECOND " <<c2 << endl; getchar(); return 0; } |
Count fot FIRST 1021 Count fot SECOND 1201
Thanks to Dheeraj for suggesting the solution.
Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Nested printf (printf inside printf) in C
- Nested functions in C
- Nested switch case
- C++ | Nested Ternary Operator
- Nested Loops in C with Examples
- How will you print numbers from 1 to 100 without using loop?
- Print 1 to 100 in C++, without loop and recursion
- Print substring of a given string without using any string function and loop in C
- How to concatenate two integer arrays without using loop in C ?
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Output of C programs | Set 56 (While loop)
- Output of C programs | Set 57 (for loop)
- How to print a number 100 times without using loop and recursion in C?
- Print a number 100 times without using loop, recursion and macro expansion in C?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Why are elementwise additions much faster in separate loops than in a combined loop?
- Difference between while and do-while loop in C, C++, Java
- C/C++ For loop with Examples
- C/C++ while loop with Examples
Improved By : nidhi_biet

