Timing C program always returns 0 seconds.
Hi,
I was looking at the programming FAQ and I saw an example method of timing a program in c. So, I tried out the example below and compiled it using the plain gcc under ubuntu. eg.
gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5). After running the compiled executable from below - I always get 0.000 seconds despite waiting a few seconds before hitting return. Would I have overlooked something?
Code:
#include <stdio.h>
#include <time.h>
int main ( void )
{
clock_t start, end;
/* Start the timer */
start = clock();
printf ( "Please wait a few moments and hit return\n" );
getchar();
/* End the timer */
end = clock();
/* Print out the difference */
printf ( "The interval was: %f seconds\n",
(double)( end - start ) / (double)CLOCKS_PER_SEC );
return 0;
}