tmm1@fe19:~$ uname -a
Linux fe19.rs.github.com 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64 GNU/Linux
tmm1@fe19:~$ cat timings.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/resource.h>
#include <time.h>
#include <math.h>
double getrusage_time() {
struct rusage usage;
struct timeval time;
getrusage(RUSAGE_SELF, &usage);
time = usage.ru_utime;
return time.tv_sec + time.tv_usec * 1e-6;
}
double clock_time() {
struct timespec ts;
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
return 0.0;
}
int main() {
int n;
printf("getrusage() before: %f\n", getrusage_time());
for (n=0; n<10000; n++) pow(2, 2048);
printf("getrusage() after: %f\n", getrusage_time());
printf("clock_gettime() before: %f\n", clock_time());
for (n=0; n<10000; n++) pow(2, 2048);
printf("clock_gettime() after: %f\n", clock_time());
}
tmm1@fe19:~$ gcc -o timings timings.c -lrt
tmm1@fe19:~$ ./timings
getrusage() before: 0.000000
getrusage() after: 0.000000
clock_gettime() before: 0.001244
clock_gettime() after: 0.001358