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
55int 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
71namespace itpp
72{
73
76
77//----------------------------------------------------------------------------
78// class Timer
79//----------------------------------------------------------------------------
81{
82 reset();
83}
84
85void Timer::start(void)
86{
87 if (!running) {
89 running = true;
90 }
91}
92
93double Timer::stop(void)
94{
95 if (running) {
98 running = false;
99 }
100
101 return elapsed_time;
102}
103
104void Timer::reset(double t)
105{
106 elapsed_time = t;
107 start_time = 0;
108 stop_time = 0;
109 running = false;
110}
111
112double Timer::get_time() const
113{
114 return running ?
117}
118
119void Timer::tic(void)
120{
121 reset();
122 start();
123}
124
125double 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
154void tic()
155{
157}
158
159double toc()
160{
161 return __tic_toc_timer.toc();
162}
163
165{
167}
168
169void 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
double get_current_time() const
Vitrual function that returns teh current time.
Definition: timing.cpp:138
A real time timer class.
Definition: timing.h:139
double get_current_time() const
Vitrual function that returns teh current time.
Definition: timing.cpp:146
double start_time
The start time of the timer.
Definition: timing.h:71
double get_time() const
Returns the elapsed time.
Definition: timing.cpp:112
void start(void)
Start the timer. This does not set the time to zero.
Definition: timing.cpp:85
bool running
A bool that indicates if the timer is running or not.
Definition: timing.h:77
double elapsed_time
The ellapsed time from start to stop.
Definition: timing.h:75
Timer()
Create a new timer. Sets the time to zero.
Definition: timing.cpp:80
virtual double get_current_time() const =0
Vitrual function that returns teh current time.
void reset(double t=0.0)
Sets the time to time t, which is zero by default. Stops the timer if it is running.
Definition: timing.cpp:104
void toc_print(void)
Prints the elapsed time since last tic()
Definition: timing.cpp:130
double stop(void)
Stop the timer. Returns the elapsed time in seconds.
Definition: timing.cpp:93
double stop_time
The stop time of the timer.
Definition: timing.h:73
double toc(void)
Returns the elapsed time since last tic()
Definition: timing.cpp:125
void tic(void)
Resets the timer and starts it.
Definition: timing.cpp:119
void pause(double t)
pause
Definition: timing.cpp:169
void toc_print()
Prints the elapsed time since last tic()
Definition: timing.cpp:164
void tic()
Reset and start timer.
Definition: timing.cpp:154
double toc()
Returns the elapsed time since last tic()
Definition: timing.cpp:159
itpp namespace
Definition: itmex.h:37
Real_Timer __tic_toc_timer
Global object for tic and toc functions.
Definition: timing.cpp:75
Definitions of Timing classes.
SourceForge Logo

Generated on Sun Jun 5 2022 21:26:42 for IT++ by Doxygen 1.9.3