Which of the following two code segments is faster? Assume that compiler makes no optimizations.
C++
/* FIRST */
for(i = 0; i < 10; i++)
for(j = 0; j < 100; j++)
//do something
// This code is contributed by Shubham Singh
C
/* FIRST */
for(i=0;i<10;i++)
for(j=0;j<100;j++)
//do something
Java
/* First */
for(i = 0; i < 10; i++)
for(j = 0; j < 100; j++)
//do something
// This code is contributed by sarajadhav12052009
Python3
# FIRST
for i in range(10):
for j in range(100):
#do something
# This code is contributed by shivani
C#
// FIRST
for(i = 0; i < 10; i++)
for(j = 0; j < 100; j++)
//do something
// This code is contributed by aditya942003patil
JavaScript
// Javascript equivalent
// Using nested for loop to loop through 10 and 100
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 100; j++) {
// do something
}
}
C++
/* SECOND */
for(i=0;i<100;i++)
for(j=0;j<10;j++)
//do something
//This code is contributed by Shubham Singh
C
/* SECOND */
for(i=0;i<100;i++)
for(j=0;j<10;j++)
//do something
Java
/* Second */
for(i = 0; i < 100; i++)
for(j = 0; j < 10; j++)
//do something
// This code is contributed by sarajadhav12052009
Python3
# SECOND
for i in range(100):
for j in range(10):
# Do something
# This code is contributed by shivani
C#
// SECOND
for(i=0;i<100;i++)
for(j=0;j<10;j++)
//do something
//This code is contributed by aditya942003patil
JavaScript
/* Second */
for(let i = 0; i < 100; i++) { // use let keyword to declare i and j
for(let 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 code counts the number of increment operations executed in FIRST and SECOND, and prints the counts.
C++
// C++ 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++);
/* SECOND */
for(int i=0; i<100; i++, c2++)
for(int j=0; j<10; j++, c2++);
cout << " Count in FIRST = " <<c1 << endl;
cout << " Count in SECOND = " <<c2 << endl;
getchar();
return 0;
}
Java
// Java program to count number of increment
// operations in FIRST and SECOND
import java.io.*;
class GFG {
public static void main (String[] args) {
int c1 = 0, c2 = 0;
for(int i=0; i<10;i++, c1++){
for(int j=0;j<100;j++, c1++){}
}
for(int i=0;i<100;i++, c2++){
for(int j=0;j<10;j++, c2++){}
}
System.out.println( "Count in First = "+c1);
System.out.println( "Count in SECOND = "+c2);
}
}
Python3
# Python program to count number of increment
# operations in FIRST and SECOND
c1 = 0
c2 = 0
# FIRST
for i in range (10):
for j in range (100):
c1 += 1
c1 += 1
# SECOND
for i in range (100):
for j in range (10):
c2 += 1
c2 += 1
print("Count in FIRST = " ,c1)
print("Count in SECOND = " ,c2)
# This code is contributed by shivanisinghss2110
C#
// C# program to count number of increment
// operations in FIRST and SECOND
using System;
class GFG{
public static void Main (String[] args)
{
int c1 = 0, c2 = 0;
for(int i = 0; i < 10; i++, c1++)
{
for(int j = 0; j < 100;j++, c1++){}
}
for(int i = 0; i < 100; i++, c2++)
{
for(int j = 0; j < 10; j++, c2++){}
}
Console.WriteLine("Count in First = " + c1);
Console.WriteLine("Count in SECOND = " + c2);
}
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
// JavaScript program to count number of increment
// operations in FIRST and SECOND
let c1 = 0, c2 = 0;
for(let i=0; i<10;i++, c1++){
for(let j=0;j<100;j++, c1++){}
}
for(let i=0;i<100;i++, c2++){
for(let j=0;j<10;j++, c2++){}
}
document.write( "Count in First = "+c1 +"<br>");
document.write( "Count in SECOND = "+c2);
// this code is contributed by shivanisinghss2110
</script>
Output Count in FIRST = 1010
Count in SECOND = 1100
Below code counts the number of comparison operations executed by FIRST and SECOND
C++
//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++);
/* SECOND */
for(int i=0; ++c2&&i<100; i++)
for(int j=0; ++c2&&j<10; j++);
cout << " Count for FIRST " <<c1 << endl;
cout << " Count for SECOND " <<c2 << endl;
getchar();
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int c1 = 0, c2 = 0;
for(int i=0; i<++c1 && i<10;i++) {
for(int j=0;j<++c1 &&j<100;j++) {
}
}
for(int i=0;i<++c2 && i<100;i++) {
for(int j=0;j<++c2 &&j<10;j++) {
}
}
System.out.println( "Count in FIRST = "+c1);
System.out.println( "Count in SECOND = "+c2);
}
}
Python3
#program to count the number of comparison
#operations executed by FIRST and SECOND */
# FIRST
c1 = 1
c2 = 1
i = 0
while (i < c1 and i < 10):
j = -1
c1 += 1
while (j < c1 and j < 100):
c1 += 1
j += 1
i += 1
# SECOND
i = 0
while (i < c2 and i < 100):
j = -1
c2 += 1
while (j < c2 and j < 10):
c2 += 1
j += 1
i += 1
print(" Count for FIRST " , c1)
print(" Count for SECOND " , c2)
# This code is contributed by shivanisinghss2110
C#
/*package whatever //do not write package name here */
using System;
class GFG {
public static void Main (String[] args) {
int c1 = 0, c2 = 0;
for(int i = 0; i < ++c1 && i < 10; i++)
{
for(int j = 0; j < ++c1 && j < 100; j++)
{
}
}
for(int i = 0; i < ++c2 && i < 100; i++) {
for(int j = 0; j < ++c2 && j < 10; j++) {
}
}
Console.WriteLine( "Count in FIRST = "+c1);
Console.WriteLine( "Count in SECOND = "+c2);
}
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
/*package whatever //do not write package name here */
//program to count the number of comparison
//operations executed by FIRST and SECOND */
let c1 = 0, c2 = 0;
for(let i=0; i<++c1 && i<10;i++) {
for(let j=0;j<++c1 &&j<100;j++) {
}
}
for(let i=0;i<++c2 && i<100;i++) {
for(let j=0;j<++c2 &&j<10;j++) {
}
}
document.write( "Count in FIRST = "+c1 +"<br>");
document.write( "Count in SECOND = "+c2);
// this code is contributed by shivanisinghss2110
</script>
Output Count for FIRST 1021
Count for 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.
Similar Reads
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin
6 min read
Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
C Arrays An array in C is a fixed-size collection of similar data items.Items are stored in contiguous memory locations. Can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc.C// A simple C
7 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C. Accessing the pointer directly will just give us the address that is stor
9 min read
Operators in C Operators are the basic components of C programming. They are symbols that represent some kind of operation, such as mathematical, relational, bitwise, conditional, or logical computations, which are to be performed on values or variables. The values and variables used with operators are called oper
11 min read
C Programming Interview Questions (2025) At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the oldest and most popular programming languages. There are many applications in which C programming language is us
15+ min read
Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are
6 min read