I am writing an application in C that requires timing between events. Now looking through <time.h> it seems that calling clock() will return the number of clock ticks of elapsed processor time. However whatever i seem to do the result is always 0.
It wouldn't be feasible to post all of my code here, but this is a small routine i wrote which shows the problem. Hopefully it is just me doing something stupid and can be fixed!
The code below just asks the user for inputs until you give it an EOF caracter (ctrl^D). The start time is always zero and for some reason, unknown to me, the end time is also always ends at zero no matter how long you leave the program running.
The code: Code:
#include <time.h>
#include <stdio.h>
int main(){
clock_t start, end;
double elapsed;
int input;
start = clock();
while(scanf("%d\n",&input)!=EOF);
end = clock();
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\nstart = %ld",start);
printf("\nend = %ld",end);
printf("\nelapsed time = %e\n",elapsed);
return 0;
}
Compiles with "gcc -ansi -pedantic -Wall <filename>.c" The output Code:
$ ./a.out
start = 0
end = 0
elapsed time = 0.000000e+00
Hoepfully someone can shed some light on what is going on here or is there another way of accurately timing the length of time something takes to execute in c
thanks