]> www.ginac.de Git - ginac.git/blob - check/fcntimer.cpp
Modification in output of last returned expression
[ginac.git] / check / fcntimer.cpp
1 /** @file fcntimer.cpp
2  *
3  *  Function execution timer. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23
24 #include <stdio.h>
25 #include <sys/resource.h>
26
27
28 // fcntimer() is a little wrapper around GiNaC's automated checks.  All those
29 // functions are passed void and return unsigned.  fcntimer() accepts one such
30 // function fcn(), returns its result and as a side-effect prints to stdout how
31 // much CPU time was consumed by fcn's execution in the fashion "(0.07s)\n".
32 unsigned fcntimer(unsigned fcn())
33 {
34     unsigned fcnresult;
35     struct rusage used1, used2;
36     double elapsed;
37
38     // time the execution of the function:
39     getrusage(RUSAGE_SELF, &used1);
40     fcnresult = fcn();
41     getrusage(RUSAGE_SELF, &used2);
42
43     // add elapsed user and system time in microseconds:
44     elapsed = ((used2.ru_utime.tv_sec - used1.ru_utime.tv_sec) +
45                (used2.ru_stime.tv_sec - used1.ru_stime.tv_sec) +
46                (used2.ru_utime.tv_usec - used1.ru_utime.tv_usec) / 1e6 +
47                (used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) / 1e6);
48
49     printf("(%.2fs)\n", elapsed);
50
51     return fcnresult;
52 }