IT++ Logo
timing.cpp
Go to the documentation of this file.
1 
29 #ifndef _MSC_VER
30 # include <itpp/config.h>
31 #else
32 # include <itpp/config_msvc.h>
33 #endif
34 
35 #ifdef TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <ctime>
38 #else
39 # ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 # else
42 # include <ctime>
43 # endif
44 #endif
45 
46 #include <itpp/base/timing.h>
47 #include <cstdio>
48 #include <iostream>
49 #include <cmath>
50 
51 
52 #if defined(_WIN32) && !defined(__CYGWIN__)
53 #include <windows.h>
54 
55 int gettimeofday(struct timeval* p, void*)
56 {
57  union {
58  long long ns100; /* time since 1 Jan 1601 in 100ns units */
59  FILETIME ft;
60  } _now;
61 
62  GetSystemTimeAsFileTime(&(_now.ft));
63  p->tv_usec = (long)((_now.ns100 / 10LL) % 1000000LL);
64  /* time since 1 Jan 1970 */
65  p->tv_sec = (long)((_now.ns100 - 116444736000000000LL) / 10000000LL);
66  return 0;
67 }
68 #endif
69 
70 
71 namespace itpp
72 {
73 
76 
77 //----------------------------------------------------------------------------
78 // class Timer
79 //----------------------------------------------------------------------------
81 {
82  reset();
83 }
84 
85 void Timer::start(void)
86 {
87  if (!running) {
89  running = true;
90  }
91 }
92 
93 double Timer::stop(void)
94 {
95  if (running) {
98  running = false;
99  }
100 
101  return elapsed_time;
102 }
103 
104 void Timer::reset(double t)
105 {
106  elapsed_time = t;
107  start_time = 0;
108  stop_time = 0;
109  running = false;
110 }
111 
112 double Timer::get_time() const
113 {
114  return running ?
116  elapsed_time;
117 }
118 
119 void Timer::tic(void)
120 {
121  reset();
122  start();
123 }
124 
125 double Timer::toc(void)
126 {
127  return get_time() ;
128 }
129 
131 {
132  std::cout << "Elapsed time = " << get_time() << " seconds" << std::endl;
133 }
134 
135 //----------------------------------------------------------------------------
136 // class CPU_Timer
137 //----------------------------------------------------------------------------
139 {
140  return static_cast<double>(clock()) / CLOCKS_PER_SEC;
141 }
142 
143 //----------------------------------------------------------------------------
144 // class Real_Timer
145 //----------------------------------------------------------------------------
147 {
148  struct timeval t;
149  gettimeofday(&t, 0);
150  return t.tv_sec + t.tv_usec * 1.0e-6;
151 }
152 
153 
154 void tic()
155 {
157 }
158 
159 double toc()
160 {
161  return __tic_toc_timer.toc();
162 }
163 
164 void toc_print()
165 {
167 }
168 
169 void pause(double t)
170 {
171  if (t == -1) {
172  std::cout << "(Press enter to continue)" << std::endl;
173  getchar();
174  }
175  else {
176  Real_Timer T;
177  T.start();
178  while (T.get_time() < t);
179  }
180 }
181 
182 } // namespace itpp
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:21 for IT++ by Doxygen 1.8.2