/
HtTime.h
128 lines (116 loc) · 2.35 KB
/
HtTime.h
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
// HtTime.h
//
// class HtTime:
// tools for timing
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtTime.h,v 1.8 2004/05/28 13:15:21 lha Exp $
//
#ifndef _HtTime_h_
#define _HtTime_h_
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef _MSC_VER /* _WIN32 */
#include <sys/timeb.h>
#endif
class HtTime
{
public:
// time in seconds (double format)
static inline double DTime()
{
#ifdef _MSC_VER /* _WIN32 */
struct timeb tb;
ftime(&tb);
return((double)((tb.millitm/1000)+tb.time+tb.timezone));
#else
struct timeval tv;
gettimeofday(&tv,NULL);
return(tv.tv_usec/1000000.0+tv.tv_sec);
#endif
}
// time in seconds relative to T0 (double format)
static inline double DTime(double T0)
{
#ifdef _MSC_VER /* _WIN32 */
struct timeb tb;
ftime(&tb);
return((double)(((tb.millitm/1000)+tb.time+tb.timezone))-T0);
#else
struct timeval tv;
gettimeofday(&tv,NULL);
return((tv.tv_usec/1000000.0+tv.tv_sec)-T0);
#endif
}
// Do something every x seconds
class Periodic
{
double t0;
double last;
double period;
public:
double total(){return(HtTime::DTime(t0));}
void change_period(double nperiod){period=nperiod;}
int operator()(double *prperiod=NULL)
{
double t=HtTime::DTime(t0);
if(prperiod){*prperiod=t-last;}
if((t-last)>period)
{
last=t;
return(1);
}
return(0);
}
Periodic(double nperiod=.1)
{
period=nperiod;
t0=HtTime::DTime();
last=0;
}
};
#ifdef NOTDEF
// print progression message every x seconds
class Progression
{
double t0;
double last;
double period;
char *label;
public:
double total(){return(HtTime::DTime()-t0);}
int operator()(double x)
{
double t=HtTime::DTime()-t0;
if((t-last)>period)
{
last=t;
printf("%s (%f): %f\n",label,t,x);
return(1);
}
return(0);
}
Progression(double nperiod=.1,char *nlabel=(char *)"progression")
{
label=nlabel;
period=nperiod;
t0=HtTime::DTime();
last=0;
}
};
#endif
};
#endif // _HtTime_h_