I am on a 64-bit Ubuntu 12.04 system and have tried the following code:
#include <unistd.h>
#include <time.h>
#include <stdio.h>
int
main(void)
{
struct timespec user1,user2;
struct timespec sys1,sys2;
double user_elapsed;
double sys_elapsed;
clock_gettime(CLOCK_REALTIME, &user1);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &sys1);
sleep(10);
clock_gettime(CLOCK_REALTIME, &user2);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &sys2);
user_elapsed = user2.tv_sec + user2.tv_nsec/1E9;
user_elapsed -= user1.tv_sec + user1.tv_nsec/1E9;
printf("CLOCK_REALTIME: %f\n", user_elapsed);
sys_elapsed = sys2.tv_sec + sys2.tv_nsec/1E9;
sys_elapsed -= sys1.tv_sec + sys1.tv_nsec/1E9;
printf("CLOCK_PROCESS_CPUTIME_ID: %f\n", sys_elapsed);
}
As I understand it, this should print something like
CLOCK_REALTIME: 10.000117
CLOCK_PROCESS_CPUTIME_ID: 10.001
But in my case, I get
CLOCK_REALTIME: 10.000117
CLOCK_PROCESS_CPUTIME_ID: 0.000032
Is this the right behavior? If so, how can I determine the actual seconds of sys1 and sys2?
When I change CLOCK_PROCESS_CPUTIME_ID to CLOCK_REALTIME, then I get the expected result, but that is not what I want, because we need precision.
[EDIT] Apparently, CLOCK_PROCESS_CPUTIME_ID returns the actual time that the processor spent processing. CLOCK_MONOTONIC returns the correct value. But with what accuracy?
source
share