clock() will [assuming that it is implemented as it should] will give you the amount of time that the CPU was busy for your particular task from one call to the next. So if you have a single(core) CPU system with two equal priority tasks that both take 1 minute to run individually [and that is completley CPU bound, so not waiting for the disk, keyboard or some other thing], clock() should report 1 minute from start to finish for each of those tasks when they are run at the same time. But the total time will be about 2 minutes.
gettimeofday will report the time that you would see on your wrist-watch, whatever your task is doing.
If we do this, the difference will be obvious:
Code:
#include <stdio.h>
long long int toddiff(struct timeval *tod1, struct timeval *tod2)
{
long long t1, t2;
t1 = tod1->tv_sec * 1000000 + tod1->tv_usec;
t2 = tod2->tv_sec * 1000000 + tod2->tv_usec;
return t1 - t2;
}
int main()
{
struct timeval tod1, tod2;
clock_t t1, t2;
t1 = clock();
// Slurp CPU for 1 second.
gettimeofday(&tod1, NULL);
do
{
gettimeofday(&tod2, NULL);
} while(toddiff(&tod2, &tod1) < 1000000);
printf("Hit enter...");
(void)getchar();
t2 = clock();
gettimeofday(&tod2, NULL);
printf("timeofday %5.2f seconds, clock %5.2f seconds\n", todiff(&tod2, &tod1) / 1000000.0, (t2-t1)/(double)CLOCKS_PER_SEC);
return 0;
}
I haven't compiled that, so I can't guarantee no typos, but concept should be OK.
--
Mats