]> www.ginac.de Git - ginac.git/blob - check/timer.cpp
Remove obsolete debug #include file from factor.cpp.
[ginac.git] / check / timer.cpp
1 /** @file timer.cpp
2  *
3  *  A simple stop watch class. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2020 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifdef HAVE_RUSAGE
24 #include <sys/resource.h>
25 #include <sys/time.h>
26 #else
27 #include <ctime>
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include "timer.h"
34
35 timer::timer() : on(false)
36 {
37 #ifdef HAVE_RUSAGE
38         getrusage(RUSAGE_SELF, &used1);
39         used2.ru_utime = used1.ru_utime;
40         used2.ru_stime = used1.ru_stime;
41 #else
42         used1 = clock();
43         used2 = used1;
44 #endif
45 }
46
47 void timer::start()
48 {
49         on = true;
50 #ifdef HAVE_RUSAGE
51         getrusage(RUSAGE_SELF, &used1);
52         used2.ru_utime = used1.ru_utime;
53         used2.ru_stime = used1.ru_stime;
54 #else
55         used1 = clock();
56         used2 = used1;
57 #endif
58 }
59
60 void timer::stop()
61 {
62         on = false;
63 #ifdef HAVE_RUSAGE
64         getrusage(RUSAGE_SELF, &used2);
65 #else
66         used2 = clock();
67 #endif
68 }
69
70 void timer::reset()
71 {
72 #ifdef HAVE_RUSAGE
73         getrusage(RUSAGE_SELF, &used1);
74         used2.ru_utime = used1.ru_utime;
75         used2.ru_stime = used1.ru_stime;
76 #else
77         used1 = clock();
78         used2 = used1;
79 #endif
80 }
81
82 double timer::read()
83 {
84 #ifdef HAVE_RUSAGE
85         if (running())
86                 getrusage(RUSAGE_SELF, &used2);
87         return ((used2.ru_utime.tv_sec - used1.ru_utime.tv_sec) +
88                 (used2.ru_stime.tv_sec - used1.ru_stime.tv_sec) +
89                 (used2.ru_utime.tv_usec - used1.ru_utime.tv_usec) * 1e-6 +
90                 (used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) * 1e-6);
91 #else
92         if (running())
93                 used2 = clock();
94         return double(used2 - used1)/CLOCKS_PER_SEC;
95 #endif
96 }
97
98 bool timer::running()
99 {
100         return on;
101 }