CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
timer.h
1 /*========================================================================
2  Timer.h : Interval timers, cycle counters, and time utilities
3  ------------------------------------------------------------------------
4  Copyright (C) 1999-2002 James R. Bruce
5  School of Computer Science, Carnegie Mellon University
6  ------------------------------------------------------------------------
7  This software is distributed under the GNU General Public License,
8  version 2. If you do not have a copy of this licence, visit
9  www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
10  Suite 330 Boston, MA 02111-1307 USA. This program is distributed
11  in the hope that it will be useful, but WITHOUT ANY WARRANTY,
12  including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  ========================================================================*/
14 
15 #ifndef __TIMER_H__
16 #define __TIMER_H__
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 
28 #include <sys/time.h>
29 #include <time.h>
30 
31 class Timer{
32  timespec tv1,tv2;
33 public:
34  void start() {clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tv1);}
35  void stop() {clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tv2);}
36  void end() {stop();}
37  double time() {return(double(tv2.tv_sec - tv1.tv_sec) + double(tv2.tv_nsec - tv1.tv_nsec) * 1.0E-9);}
38  double timeMSec() {return(time() * 1.0E3);}
39  double timeUSec() {return(time() * 1.0E6);}
40 
41  double interval(){
42  double t;
43  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tv2);
44  t = time();
45  tv1 = tv2;
46  return(t);
47  }
48  double midtime() {
49  timespec tmp;
50  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tmp);
51  return(double(tmp.tv_sec - tv1.tv_sec) + double(tmp.tv_nsec - tv1.tv_nsec) * 1.0E-9);
52  }
53 };
54 
56  timespec tv1,tv2;
57  double total;
58 public:
60  clear();
61  }
62  void clear() {
63  total=0.0;
64  }
65  void start() {clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tv1);}
66  void stop()
67  {
68  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tv2);
69  total+=time();
70  }
71  double getTotal() {return total;}
72  void end() {stop();}
73  double time() {return(double(tv2.tv_sec - tv1.tv_sec) + double(tv2.tv_nsec - tv1.tv_nsec) * 1.0E-9);}
74  double timeMSec() {return(time() * 1.0E3);}
75  double timeUSec() {return(time() * 1.0E6);}
76 
77  double interval(){
78  double t;
79  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tv2);
80  t = time();
81  tv1 = tv2;
82  return(t);
83  }
84  double midtime() {
85  timespec tmp;
86  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tmp);
87  return(double(tmp.tv_sec - tv1.tv_sec) + double(tmp.tv_nsec - tv1.tv_nsec) * 1.0E-9);
88  }
89 };
90 
91 inline unsigned GetTimeUSec()
92 {
93 #ifdef Apertos
94  struct SystemTime time;
95  GetSystemTime(&time);
96  return(time.seconds*1000000 + time.useconds);
97 #else
98  timespec ts;
99  clock_gettime(CLOCK_REALTIME,&ts);
100  return(double(ts.tv_sec)*1000000.0 + double(ts.tv_nsec)*(1.0E-3));
101 #endif
102 }
103 
104 inline double GetTimeSec()
105 {
106 #ifdef Apertos
107  struct SystemTime time;
108  GetSystemTime(&time);
109  return((double)time.seconds + time.useconds*(1.0E-6));
110 #else
111  timespec ts;
112  clock_gettime(CLOCK_REALTIME,&ts);
113  return(double(ts.tv_sec) + double(ts.tv_nsec)*(1.0E-9));
114 #endif
115 }
116 
117 inline double GetProcessTimeSec()
118 {
119  timespec ts;
120  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&ts);
121  return(double(ts.tv_sec) + double(ts.tv_nsec)*(1.0E-9));
122 }
123 
124 inline void GetDate(struct tm &date)
125 {
126  time_t t = time(NULL);
127  localtime_r(&t,&date);
128 }
129 
130 inline void Sleep(double sec)
131 {
132  usleep((int)(sec * 1E6));
133 }
134 
136  const char *fname;
137  double tStart, lapStart, tStop;
138  int lapNum;
139  double getTickCount()
140  {
141  timespec ts;
142  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&ts);
143  return(double(ts.tv_sec) + double(ts.tv_nsec)*(1.0E-9));
144  }
145 public:
146  FunctionTimer(const char *_fname)
147  {fname=_fname; tStart = lapStart = getTickCount(); lapNum = 0; tStop=0.0;}
148  void Lap(int lineNum)
149  {tStop=getTickCount(); printf("%s line %d lap %d: %9.3fus\n",fname,lineNum,lapNum,(tStop-lapStart)*1e6); lapNum++; lapStart=tStop;}
150  ~FunctionTimer()
151  {tStop=getTickCount(); printf("%s: %0.6fms\n",fname,(tStop-tStart)*1000.0);}
152 };
153 
154 
155 #endif
Definition: timer.h:31