I checked the following two programs with DDD(digital dispaly
debugger on linux). In for loop, the index variable i is always kept
in EAX
register but in while loop it was kept in EAX register only at the
time where the actual use of index variable(i.e at the time of
incrementing index
variable) and after the increment some other variable using EAX
register. In next iteration variable i is stored back to EAX.
[snippage]
So from the above experiment for loop executes faster than while loop.
The above conclusion is based on the assumtion that the register
access is
faster than memory access.
As I think Dan Pop noted, that only tests one particular implementation;
the results cannot be generalized to other implementations. I find
it curious, however, given what I know about the internals of gcc,
that you found any difference at all. Here are your two programs
(with some whitespace compression for news-posting-reasons, though
the versions I compiled did not even have that), plus the difference
between the results of compiling them.
Both were compiled with and without optimization, using gcc (3.2.3)
for.c
#include <stdio.h>
int main() {
int i,n=10,sum=0;
for(i = 0; i < n; i++) { sum += i; }
}
while.c
#include <stdio.h>
int main()
{
int i,n=10,sum=0;
i=0; while(i < n) { sum += i; i++; }
}
% cd /tmp
% cc -S for.c while.c
% diff for.s while.s
1c1
< .file "for.c"
---
18c18
< jl .L5
---
20c20
< .L5:
---
% cc -S -O4 -mregparm=3 -fomit-frame-pointer for.c while.c
% diff for.s while.s
1c1
< .file "for.c"
---
13c13
< .L6:
---
15c15
< jns .L6
---
As this shows, the only difference is the name of the file in
the .file directive, and the name of the local label controlling
the loop. In the optimized code, the actual loop is just:
.Ln: decl %eax; jns .Ln
Since the sum is not used, the variable that holds it has been
discarded, and the loop has been transformed from "i counts up from
0 to 9 inclusive" to "i counts down from 9 to 0 inclusive".