]> www.ginac.de Git - ginac.git/blob - check/fcntimer.cpp
- switched to automake build environment
[ginac.git] / check / fcntimer.cpp
1 // check/fcntimer.cpp
2
3 #include <stdio.h>
4 #include <sys/resource.h>
5
6
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())
12 {
13     unsigned fcnresult;
14     struct rusage used1, used2;
15     double elapsed;
16
17     // time the execution of the function:
18     getrusage(RUSAGE_SELF, &used1);
19     fcnresult = fcn();
20     getrusage(RUSAGE_SELF, &used2);
21
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);
27
28     printf("(%.2fs)\n", elapsed);
29
30     return fcnresult;
31 }