Apparently, on Unix (FreeBSD), clock() returns CPU seconds, not elapsed seconds.
This is the code I used in one of my apps. It starts off with this at the front:
Code:
// This is for elapsed time calculations.
char hours[3], minutes[3], seconds[3] ;
struct tm start_time ;
struct tm *the_time ;
time_t stime, etime ;
double elapsed ;
int elap_hour, elap_min, elap_sec ;
time(&stime) ; // Get start time in seconds since epoch
the_time = localtime(&stime) ;
// Save the Time of Day
start_time.tm_hour = the_time->tm_hour ;
start_time.tm_min = the_time->tm_min ;
start_time.tm_sec = the_time->tm_sec ;
// End of elapsed time stuff, for now.
At the end, I do this:
Code:
time(&etime) ; // Get end time in seconds since epoch
the_time = localtime(&etime) ;
sprintf(hours, "%02d", the_time->tm_hour ) ;
sprintf(minutes, "%02d", the_time->tm_min ) ;
sprintf(seconds, "%02d", the_time->tm_sec ) ;
cout << "End Time: " << hours << ":" << minutes << ":" << seconds << endl;
sprintf(hours, "%02d", start_time.tm_hour ) ;
sprintf(minutes, "%02d", start_time.tm_min ) ;
sprintf(seconds, "%02d", start_time.tm_sec ) ;
cout << "Start Time: " << hours << ":" << minutes << ":" << seconds << endl;
// Calculate the elapsed time
elapsed = difftime(etime, stime) ;
//printf("Elapsed time is %d %s.\n", (int) elapsed, ((int) elapsed == 1 ) ? "second" : "seconds" ) ;
elap_hour = (int) (elapsed / (60*60)) ; // hours
elap_min = (int) (elapsed / 60) % 60 ; // minutes
elap_sec = (int) elapsed % (60) ; // seconds
// printf("Elapsed (int) time is %10d\n" , (int) elapsed ) ;
printf("Elapsed Time: %02d:%02d:%02d\n", elap_hour, elap_min ,elap_sec ) ;
(Yes, I know it's ugly - but it works )