-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
156 lines (123 loc) · 4.7 KB
/
timer.c
File metadata and controls
156 lines (123 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*************************************************************************
Simple timer functions.
---------------------------------------------------------------------
Copyright (c) 2005, 2006, 2007 Manuel Lopez-Ibanez
TeX: \copyright 2005, 2006, 2007 Manuel L{\'o}pez-Ib{\'a}{\~n}ez
This program is free software (software libre); you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, you can obtain a copy of the GNU
General Public License at:
http://www.gnu.org/copyleft/gpl.html
or by writing to:
Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA
----------------------------------------------------------------------
NOTES: based on source code from Thomas Stuetzle.
*************************************************************************/
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "timer.h"
#define TIMER_CPUTIME(X) ( (double)X.ru_utime.tv_sec + \
(double)X.ru_stime.tv_sec + \
((double)X.ru_utime.tv_usec + \
(double)X.ru_stime.tv_usec ) * 1.0E-6)
#define TIMER_WALLTIME(X) ( (double)X.tv_sec + \
(double)X.tv_usec * 1.0E-6 )
static struct rusage res;
static struct timeval tp;
static double virtual_time, real_time;
static double stop_virtual_time, stop_real_time;
/*
* The virtual time of day and the real time of day are calculated and
* stored for future use. The future use consists of subtracting these
* values from similar values obtained at a later time to allow the user
* to get the amount of time used by the backtracking routine.
*/
void Timer_start()
{
gettimeofday( &tp, NULL );
real_time = TIMER_WALLTIME(tp);
getrusage( RUSAGE_SELF, &res );
virtual_time = TIMER_CPUTIME(res);
}
/*
* Return the time used in seconds (either
* REAL or VIRTUAL time, depending on ``type'').
*/
double Timer_elapsed_virtual (void)
{
double timer_tmp_time;
getrusage (RUSAGE_SELF, &res);
timer_tmp_time = TIMER_CPUTIME(res) - virtual_time;
#if DEBUG >= 4
if (timer_tmp_time < 0.0) {
fprintf(stderr, "%s: Timer_elapsed(): warning: "
"negative increase in time ", __FILE__);
fprintf(stderr, "(%.6g - %.6g = ",
TIMER_CPUTIME(res) , virtual_time);
fprintf(stderr, "%.6g)\n", timer_tmp_time);
}
#endif
return (timer_tmp_time < 0.0) ? 0 : timer_tmp_time;
}
double Timer_elapsed_real (void)
{
double timer_tmp_time;
gettimeofday (&tp, NULL);
timer_tmp_time = TIMER_WALLTIME(tp) - real_time;
#if DEBUG >= 2
if (timer_tmp_time < 0.0) {
fprintf(stderr, "%s: Timer_elapsed(): warning: "
"negative increase in time ", __FILE__);
fprintf(stderr, "(%.6g - %.6g = ",
TIMER_WALLTIME(tp) , real_time);
fprintf(stderr, "%.6g)\n", timer_tmp_time);
}
#endif
return (timer_tmp_time < 0.0) ? 0 : timer_tmp_time;
}
double Timer_elapsed( TIMER_TYPE type )
{
return (type == REAL_TIME)
? Timer_elapsed_real ()
: Timer_elapsed_virtual ();
}
void Timer_stop(void)
{
gettimeofday( &tp, NULL );
stop_real_time = TIMER_WALLTIME(tp);
getrusage( RUSAGE_SELF, &res );
stop_virtual_time = TIMER_CPUTIME(res);
}
void Timer_continue(void)
{
double timer_tmp_time;
gettimeofday( &tp, NULL );
timer_tmp_time = TIMER_WALLTIME(tp) - stop_real_time;
#if DEBUG >= 2
if (timer_tmp_time < 0.0) {
fprintf(stderr, "%s: Timer_continue(): warning: "
"negative increase in time (%.6g - %.6g = %.6g)\n",
__FILE__, TIMER_WALLTIME(tp), stop_real_time, timer_tmp_time);
}
#endif
if (timer_tmp_time > 0.0) real_time += timer_tmp_time;
getrusage( RUSAGE_SELF, &res );
timer_tmp_time = TIMER_CPUTIME(res) - stop_virtual_time;
#if DEBUG >= 2
if (timer_tmp_time < 0.0) {
fprintf(stderr, "%s: Timer_continue(): warning: "
"negative increase in time (%.6g - %.6g = %.6g)\n",
__FILE__, TIMER_CPUTIME(res),stop_virtual_time,timer_tmp_time);
}
#endif
if (timer_tmp_time > 0.0) virtual_time += timer_tmp_time;
}