4 #include <sys/resource.h>
7 // fcntimer() is a little wrapper around GiNaC's automated checks. All those
8 // functions are passed void and return unsigned. fcntimer() accepts one such
9 // function fcn(), returns its result and as a side-effect prints to stdout how
10 // much CPU time was consumed by fcn's execution in the fashion "(0.07s)\n".
11 unsigned fcntimer(unsigned fcn())
14 struct rusage used1, used2;
17 // time the execution of the function:
18 getrusage(RUSAGE_SELF, &used1);
20 getrusage(RUSAGE_SELF, &used2);
22 // add elapsed user and system time in microseconds:
23 elapsed = ((used2.ru_utime.tv_sec - used1.ru_utime.tv_sec) +
24 (used2.ru_stime.tv_sec - used1.ru_stime.tv_sec) +
25 (used2.ru_utime.tv_usec - used1.ru_utime.tv_usec) / 1e6 +
26 (used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) / 1e6);
28 printf("(%.2fs)\n", elapsed);