00001 // HtTime.h 00002 // 00003 // class HtTime: 00004 // tools for timing 00005 // 00006 // Part of the ht://Dig package <http://www.htdig.org/> 00007 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group 00008 // For copyright details, see the file COPYING in your distribution 00009 // or the GNU General Public License version 2 or later 00010 // <http://www.gnu.org/copyleft/gpl.html> 00011 // 00012 // $Id: HtTime_8h-source.html,v 1.1 2008年06月08日 10:12:54 sebdiaz Exp $ 00013 // 00014 #ifndef _HtTime_h_ 00015 #define _HtTime_h_ 00016 00017 #if TIME_WITH_SYS_TIME 00018 #include <sys/time.h> 00019 #include <time.h> 00020 #else 00021 # if HAVE_SYS_TIME_H 00022 # include <sys/time.h> 00023 # else 00024 # include <time.h> 00025 # endif 00026 #endif 00027 00028 00029 class HtTime 00030 { 00031 public: 00032 // time in seconds (double format) 00033 static inline double DTime() 00034 { 00035 struct timeval tv; 00036 gettimeofday(&tv,NULL); 00037 return(tv.tv_usec/1000000.0+tv.tv_sec); 00038 } 00039 // time in seconds relative to T0 (double format) 00040 static inline double DTime(double T0) 00041 { 00042 struct timeval tv; 00043 gettimeofday(&tv,NULL); 00044 return((tv.tv_usec/1000000.0+tv.tv_sec)-T0); 00045 } 00046 00047 // Do something every x seconds 00048 class Periodic 00049 { 00050 double t0; 00051 double last; 00052 double period; 00053 public: 00054 double total(){return(HtTime::DTime(t0));} 00055 void change_period(double nperiod){period=nperiod;} 00056 int operator()(double *prperiod=NULL) 00057 { 00058 double t=HtTime::DTime(t0); 00059 if(prperiod){*prperiod=t-last;} 00060 if((t-last)>period) 00061 { 00062 last=t; 00063 return(1); 00064 } 00065 return(0); 00066 } 00067 Periodic(double nperiod=.1) 00068 { 00069 period=nperiod; 00070 t0=HtTime::DTime(); 00071 last=0; 00072 } 00073 }; 00074 00075 00076 00077 #ifdef NOTDEF 00078 // print progression message every x seconds 00079 class Progression 00080 { 00081 double t0; 00082 double last; 00083 double period; 00084 char *label; 00085 public: 00086 double total(){return(HtTime::DTime()-t0);} 00087 int operator()(double x) 00088 { 00089 double t=HtTime::DTime()-t0; 00090 if((t-last)>period) 00091 { 00092 last=t; 00093 printf("%s (%f): %f\n",label,t,x); 00094 return(1); 00095 } 00096 return(0); 00097 } 00098 Progression(double nperiod=.1,char *nlabel="progression") 00099 { 00100 label=nlabel; 00101 period=nperiod; 00102 t0=HtTime::DTime(); 00103 last=0; 00104 } 00105 }; 00106 #endif 00107 }; 00108 #endif // _HtTime_h_ 00109 00110 00111 00112