I wrote following program in C to calculate the execution time (On a Windows-7 machine):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
double time_spent = 0.0;
clock_t begin;
clock_t end;
begin = clock();
// code to read a file
end = clock();
time_spent += ((double)(end - begin)) / CLOCKS_PER_SEC;
printf("Time elapsed is %f seconds", time_spent);
getch();
return 0;
}
At the end of the execution, the value of the (end-begin)
is negative.
Also, the value of begin
was 46 while that of end
was 43 after the program execution.
I am using a 64-bit Windows-7.
- Can someone please explain why the value of
end
is smaller thanbegin
. - How can this be fixed on a windows machine.
(clock() returning a negative value in C talks about POSIX only).