IT++
4.3.1
Toggle main menu visibility
itpp
base
timing.cpp
Go to the documentation of this file.
1
28
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
75
Real_Timer
__tic_toc_timer
;
76
77
//----------------------------------------------------------------------------
78
// class Timer
79
//----------------------------------------------------------------------------
80
Timer::Timer
()
81
{
82
reset
();
83
}
84
85
void
Timer::start
(
void
)
86
{
87
if
(!
running
) {
88
start_time
=
get_current_time
();
89
running
=
true
;
90
}
91
}
92
93
double
Timer::stop
(
void
)
94
{
95
if
(
running
) {
96
stop_time
=
get_current_time
();
97
elapsed_time
+=
stop_time
-
start_time
;
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
?
115
elapsed_time
+
get_current_time
() -
start_time
:
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
130
void
Timer::toc_print
(
void
)
131
{
132
std::cout <<
"Elapsed time = "
<<
get_time
() <<
" seconds"
<< std::endl;
133
}
134
135
//----------------------------------------------------------------------------
136
// class CPU_Timer
137
//----------------------------------------------------------------------------
138
double
CPU_Timer::get_current_time
()
const
139
{
140
return
static_cast<
double
>
(clock()) / CLOCKS_PER_SEC;
141
}
142
143
//----------------------------------------------------------------------------
144
// class Real_Timer
145
//----------------------------------------------------------------------------
146
double
Real_Timer::get_current_time
()
const
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
{
156
__tic_toc_timer
.tic();
157
}
158
159
double
toc
()
160
{
161
return
__tic_toc_timer
.toc();
162
}
163
164
void
toc_print
()
165
{
166
__tic_toc_timer
.toc_print();
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
itpp::CPU_Timer::get_current_time
double get_current_time() const
Vitrual function that returns teh current time.
Definition
timing.cpp:138
itpp::Real_Timer
A real time timer class.
Definition
timing.h:139
itpp::Real_Timer::get_current_time
double get_current_time() const
Vitrual function that returns teh current time.
Definition
timing.cpp:146
itpp::Timer::start_time
double start_time
The start time of the timer.
Definition
timing.h:71
itpp::Timer::get_time
double get_time() const
Returns the elapsed time.
Definition
timing.cpp:112
itpp::Timer::start
void start(void)
Start the timer. This does not set the time to zero.
Definition
timing.cpp:85
itpp::Timer::running
bool running
A bool that indicates if the timer is running or not.
Definition
timing.h:77
itpp::Timer::elapsed_time
double elapsed_time
The ellapsed time from start to stop.
Definition
timing.h:75
itpp::Timer::Timer
Timer()
Create a new timer. Sets the time to zero.
Definition
timing.cpp:80
itpp::Timer::get_current_time
virtual double get_current_time() const =0
Vitrual function that returns teh current time.
itpp::Timer::reset
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
itpp::Timer::toc_print
void toc_print(void)
Prints the elapsed time since last tic().
Definition
timing.cpp:130
itpp::Timer::stop
double stop(void)
Stop the timer. Returns the elapsed time in seconds.
Definition
timing.cpp:93
itpp::Timer::stop_time
double stop_time
The stop time of the timer.
Definition
timing.h:73
itpp::Timer::toc
double toc(void)
Returns the elapsed time since last tic().
Definition
timing.cpp:125
itpp::Timer::tic
void tic(void)
Resets the timer and starts it.
Definition
timing.cpp:119
itpp::pause
void pause(double t)
pause
Definition
timing.cpp:169
itpp::toc_print
void toc_print()
Prints the elapsed time since last tic().
Definition
timing.cpp:164
itpp::tic
void tic()
Reset and start timer.
Definition
timing.cpp:154
itpp::toc
double toc()
Returns the elapsed time since last tic().
Definition
timing.cpp:159
itpp
itpp namespace
Definition
itmex.h:37
itpp::__tic_toc_timer
Real_Timer __tic_toc_timer
Global object for tic and toc functions.
Definition
timing.cpp:75
timing.h
Definitions of Timing classes.
Generated by
1.17.0