1 // HtTime.h
2 //
3 // class HtTime:
4 // tools for timing
5 //
6 // Part of the ht://Dig package   <http://www.htdig.org/>
7 // Copyright (c) 1999-2004 The ht://Dig Group
8 // For copyright details, see the file COPYING in your distribution
9 // or the GNU Library General Public License (LGPL) version 2 or later
10 // <http://www.gnu.org/copyleft/lgpl.html>
11 //
12 // $Id: HtTime.h,v 1.8 2004/05/28 13:15:21 lha Exp $
13 //
14 #ifndef _HtTime_h_
15 #define _HtTime_h_
16 
17 #if TIME_WITH_SYS_TIME
18 #include <sys/time.h>
19 #include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27 
28 #ifdef _MSC_VER /* _WIN32 */
29 #include <sys/timeb.h>
30 #endif
31 
32 class HtTime
33 {
34  public:
35     // time in seconds (double format)
DTime()36     static inline double DTime()
37     {
38 #ifdef _MSC_VER /* _WIN32 */
39 	struct timeb tb;
40 	ftime(&tb);
41 	return((double)((tb.millitm/1000)+tb.time+tb.timezone));
42 #else
43 	struct timeval tv;
44 	gettimeofday(&tv,NULL);
45 	return(tv.tv_usec/1000000.0+tv.tv_sec);
46 #endif
47 
48     }
49     // time in seconds relative to T0 (double format)
DTime(double T0)50     static inline double DTime(double T0)
51     {
52 #ifdef _MSC_VER /* _WIN32 */
53 	struct timeb tb;
54 	ftime(&tb);
55 	return((double)(((tb.millitm/1000)+tb.time+tb.timezone))-T0);
56 #else
57 	struct timeval tv;
58 	gettimeofday(&tv,NULL);
59 	return((tv.tv_usec/1000000.0+tv.tv_sec)-T0);
60 #endif
61     }
62 
63     // Do something every x seconds
64     class Periodic
65     {
66 	double t0;
67 	double last;
68 	double period;
69     public:
total()70 	double total(){return(HtTime::DTime(t0));}
change_period(double nperiod)71 	void change_period(double nperiod){period=nperiod;}
operator()72 	int operator()(double *prperiod=NULL)
73 	{
74 	    double t=HtTime::DTime(t0);
75 	    if(prperiod){*prperiod=t-last;}
76 	    if((t-last)>period)
77 	    {
78 		last=t;
79 		return(1);
80 	    }
81 	    return(0);
82 	}
83 	Periodic(double nperiod=.1)
84 	{
85 	    period=nperiod;
86 	    t0=HtTime::DTime();
87 	    last=0;
88 	}
89     };
90 
91 
92 
93 #ifdef NOTDEF
94     // print progression message every x seconds
95     class Progression
96     {
97 	double t0;
98 	double last;
99 	double period;
100 	char *label;
101     public:
total()102 	double total(){return(HtTime::DTime()-t0);}
operator()103 	int operator()(double x)
104 	{
105 	    double t=HtTime::DTime()-t0;
106 	    if((t-last)>period)
107 	    {
108 		last=t;
109 		printf("%s (%f): %f\n",label,t,x);
110 		return(1);
111 	    }
112 	    return(0);
113 	}
114 	Progression(double nperiod=.1,char *nlabel=(char *)"progression")
115 	{
116 	    label=nlabel;
117 	    period=nperiod;
118 	    t0=HtTime::DTime();
119 	    last=0;
120 	}
121     };
122 #endif
123 };
124 #endif // _HtTime_h_
125 
126 
127 
128 
129