1. ## Execution time problem.

We need to sum n numbers using for,while, and do while loops..
and need to compare the execution time for each of them.
I wrote the following code.
but every time the output comes out to be different!!
so not able to decide which loop executes the fastest.
kindly help me figure this out..

Code:
```#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
#define n 20000
int sumfor();
int sumwhile();
int sumdowhile();

int a[n];
time_t first,second;
double tfor,twhile,tdowhile;
void main()
{
int i,s1,s2,s3;

// initializing the array..
for(i=0;i<n;i++)
{
a[i]=1;
}
}

s1=sumfor();
s2=sumwhile();
s3=sumdowhile();

if ((s1==s2) && (s2==s3))
printf("\nsum is %d",s1);
else
printf("\nerror!!!");

printf("\ntime with for loop %f",tfor);

printf("\ntime with while loop %f",twhile);

printf("\ntime with do-while loop %f",tdowhile);

getch();
}

int sumfor()
{ int i,p=0;

printf("\nRunning for loop");
first=time(NULL);

for(i=0;i<n;i++)
{
p=p+a[i];
}

second=time(NULL);

tfor=difftime(second,first);

return p;
}

int sumwhile()
{
int i,p;
i=0;
p=0;

printf("\nRunning for while loop");
first=time(NULL);

while(i<n)
{
p=p+a[i];
i++;

}
second=time(NULL);
twhile=difftime(second,first);
return p;
}

int sumdowhile()
{
int i,p;
i=0;
p=0;

printf("\nRunning for do while loop");

first=time(NULL);
do
{
p=p+a[i];
i++;
}while(i<n);
second=time(NULL);
tdowhile=difftime(second,first);
return p;
}```

2. You have the main returning void.Also the parses do not seem correct to me.I think the the } in line 25 is wrong.

However it is not clear to me.Your calculations result in a different result with each loop?

EDIT->in the do while you increase value of i and then print,so it says that you added 1st element and finally the 200-th element,but in the other two(for,while) you start from 0-th element and end to 199-th element.Maybe you want to correct it.

I got that the for loop is faster.If they have they same time for you,you may increase value of n

3. Presumably your teacher told you how to do the timing, and presumably it is not the way you're doing it. The problems with your approach is that it only has one-second resolution and that it measures wall-clock time which makes it useless.

Are you on a *nix system? (Linux or BSD, for instance?) If so, try the "time" command:

\$ time your_program

And as std10093 has pointed out, your program as posted will not even compile because of mismatched braces. E.g.,
Code:
```for(i=0;i<n;i++)
{
a[i]=1;
}
}```

4. Is there a reason you've posted C on the C++ board?

5. Moved to C board.

A couple of points.

SourceForge.net: Indentation - cpwiki

2. printf("\n adding %d th element",i);
This will seriously screw up the timings.
Summing 20000 integers (in any method) will take no time at all in comparison to the time it takes to do all those printf calls.

To generate something like meaningful times when all you have is a slow stopwatch, you need to do something like
Code:
```t1 = time()
for ( i = 0 ; i < 1000 ; i++ ) {
for ( j = 0 ; j < n ; j++ ) {
// test code
}
}
t2 = time()
result = difftime(t2,t2) / 1000;```
The outer loop runs the test code 1000 times.
Then we divide the total time by 1000 to get an average for just one iteration.

6. for highest resolution timing, in linux use clock_gettime with the monotonic option. in windows, use QueryPerformanceCounter and QueryPerformance Frequency