Compare the Performance of For and While Loops in C
In C, the for
and while
loops are used for iteration, but their performance can vary based on the use case. Generally, a for
loop is more structured and optimized for known iterations, whereas a while
loop is more flexible when the stopping condition is not predetermined. To compare their performance, we measure execution time in different scenarios.
Example 1: Comparing Execution Time for a Fixed Number of Iterations
In this example, we will compare the execution time of a for
loop and a while
loop running the same number of iterations. We use the clock()
function from time.h
to measure the time taken by each loop.
main.c
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double time_for, time_while;
int i;
// Measuring for loop execution time
start = clock();
for (i = 0; i < 1000000; i++);
end = clock();
time_for = ((double)(end - start)) / CLOCKS_PER_SEC;
// Measuring while loop execution time
i = 0;
start = clock();
while (i < 1000000) {
i++;
}
end = clock();
time_while = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("For Loop Time: %f seconds\n", time_for);
printf("While Loop Time: %f seconds\n", time_while);
return 0;
}
Explanation:
- We include
time.h
for measuring execution time using theclock()
function. - The
clock_t
variablesstart
andend
store time stamps before and after loop execution. - The
for
loop runs for 1,000,000 iterations without any operation inside. - The
while
loop performs the same number of iterations with an explicit increment operation. - Execution time for both loops is calculated as
(end - start) / CLOCKS_PER_SEC
to get seconds. - The results are printed to compare which loop executes faster.
Output:
For Loop Time: 0.002230 seconds
While Loop Time: 0.002224 seconds
Example 2: Comparing Loop Performance with Computational Work
In this example, both loops perform a summation operation, adding numbers from 1 to 100,000, and we compare their execution times.
main.c
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double time_for, time_while;
long long sum;
int i;
// Using for loop
sum = 0;
start = clock();
for (i = 1; i <= 100000; i++) {
sum += i;
}
end = clock();
time_for = ((double)(end - start)) / CLOCKS_PER_SEC;
// Using while loop
sum = 0;
i = 1;
start = clock();
while (i <= 100000) {
sum += i;
i++;
}
end = clock();
time_while = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("For Loop Time: %f seconds\n", time_for);
printf("While Loop Time: %f seconds\n", time_while);
return 0;
}
Explanation:
- The variable
sum
is initialized to store the summation of numbers. - The
for
loop iterates from 1 to 100,000, adding each number tosum
. - The
while
loop does the same operation, incrementingi
explicitly. - Execution times for both loops are calculated and displayed.
Output:
For Loop Time: 0.000240 seconds
While Loop Time: 0.000246 seconds
Conclusion
From the above examples, we can observe:
for
loops tend to be slightly faster in structured iterations because the iteration variable is updated internally.while
loops offer more flexibility but might introduce extra instructions for incrementing variables manually.- Performance differences are minor for small loops, but in performance-critical applications, using the correct loop structure is important.
Use for
loops when the number of iterations is known and while
loops when iteration depends on a condition.