FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
time_stamp.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
10#include <kernel/util/string.hpp>
12#include <kernel/util/os_windows.hpp>
13
14// includes, system
15#include <sstream>
16#include <iomanip>
17
18#if defined(__linux) || defined(__unix__)
19# define FEAT_HAVE_GETTIMEOFDAY 1
20# include <sys/time.h>
21#else
22# include <ctime>
23#endif
24
25namespace FEAT
26{
30 enum class TimeFormat
31 {
32 h_m_s_m,
33 h_m_s,
34 m_s_m,
35 m_s,
36 s_m,
37 };
38
54 {
55 private:
57#if defined(FEAT_HAVE_GETTIMEOFDAY)
58 timeval _time;
59#elif defined(_WIN32)
60 long long int _counter;
61#else
62 clock_t _clock;
63#endif
64
65 public:
68 {
69 stamp();
70 }
71
80 {
81#if defined(FEAT_HAVE_GETTIMEOFDAY)
82 gettimeofday(&_time, 0);
83#elif defined(_WIN32)
85#else
86 _clock = ::clock();
87#endif
88 return *this;
89 }
90
100 double elapsed(const TimeStamp& before) const
101 {
102#if defined(FEAT_HAVE_GETTIMEOFDAY)
103 return double(_time.tv_sec - before._time.tv_sec) + 1E-6 * double(_time.tv_usec - before._time.tv_usec);
104#elif defined(_WIN32)
105 long long freq = Windows::query_performance_frequency();
106 return (freq == 0ll) ? 0.0 : (double(_counter - before._counter) / double(freq));
107#else
108 return double(_clock - before._clock) / double(CLOCKS_PER_SEC);
109#endif
110 }
111
121 double elapsed_now() const
122 {
123 return TimeStamp().elapsed(*this);
124 }
125
135 long long elapsed_micros(const TimeStamp& before) const
136 {
137#if defined(FEAT_HAVE_GETTIMEOFDAY)
138 return 1000000ll * (long long)(_time.tv_sec - before._time.tv_sec)
139 + (long long)(_time.tv_usec - before._time.tv_usec);
140#elif defined(_WIN32)
141 long long freq = Windows::query_performance_frequency();
142 return (freq == 0ll) ? 0ll : (1000000ll * (_counter - before._counter)) / freq;
143#else
144 return 1000000ll * (long long)(_clock - before._clock) / (long long)CLOCKS_PER_SEC;
145#endif
146 }
147
157 long long elapsed_micros_now() const
158 {
159 return TimeStamp().elapsed_micros(*this);
160 }
161
187 static String format_micros(long long micros, TimeFormat format = TimeFormat::s_m)
188 {
189 std::ostringstream oss;
190 oss << std::setfill('0');
191
192 // check whether the time is negative
193 if(micros < 0)
194 {
195 oss << '-';
196 micros = -micros;
197 }
198
199 switch (format)
200 {
202 oss << (micros / 3600000000ll);
203 oss << ":" << std::setw(2) << ((micros / 60000000ll) % 60ll);
204 oss << ":" << std::setw(2) << ((micros / 1000000ll) % 60ll);
205 oss << "." << std::setw(3) << ((micros / 1000ll) % 1000ll);
206 break;
207
209 oss << (micros / 3600000000ll);
210 oss << ":" << std::setw(2) << ((micros / 60000000ll) % 60ll);
211 oss << ":" << std::setw(2) << ((micros / 1000000ll) % 60ll);
212 break;
213
215 oss << (micros / 60000000ll);
216 oss << ":" << std::setw(2) << ((micros / 1000000ll) % 60ll);
217 oss << "." << std::setw(3) << ((micros / 1000ll) % 1000ll);
218 break;
219
220 case TimeFormat::m_s:
221 oss << (micros / 60000000ll);
222 oss << ":" << std::setw(2) << ((micros / 1000000ll) % 60ll);
223 break;
224
225 case TimeFormat::s_m:
226 oss << (micros / 1000000ll);
227 oss << "." << std::setw(3) << ((micros / 1000ll) % 1000ll);
228 break;
229
230 default:
231 XABORTM("TimeFormat not supported!");
232 }
233
234 // return formatted string
235 return oss.str();
236 }
237
254 {
255 return format_micros(elapsed_micros(before), format);
256 }
257
274 {
275 return TimeStamp().elapsed_string(*this, format);
276 }
277
287 bool operator< (const TimeStamp & other) const
288 {
289#if defined(FEAT_HAVE_GETTIMEOFDAY)
290 return (_time.tv_sec < other._time.tv_sec) ||
291 ((_time.tv_sec == other._time.tv_sec) && (_time.tv_usec < other._time.tv_usec));
292#elif defined(_WIN32)
293 return (_counter < other._counter);
294#else
295 return (_clock < other._clock);
296#endif
297 }
298 }; // class TimeStamp
299} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
FEAT Kernel base header.
String class implementation.
Definition: string.hpp:46
Time stamp class.
Definition: time_stamp.hpp:54
static String format_micros(long long micros, TimeFormat format=TimeFormat::s_m)
Formats an elapsed time in microseconds as a string.
Definition: time_stamp.hpp:187
TimeStamp & stamp()
Stamps the current time-stamp.
Definition: time_stamp.hpp:79
TimeStamp()
Constructor.
Definition: time_stamp.hpp:67
String elapsed_string(const TimeStamp &before, TimeFormat format=TimeFormat::s_m) const
Return the time elapsed between two time stamps as a string.
Definition: time_stamp.hpp:253
clock_t _clock
Our time-stamp.
Definition: time_stamp.hpp:62
String elapsed_string_now(TimeFormat format=TimeFormat::s_m) const
Calculates the time elapsed between the time stamp and now as a string.
Definition: time_stamp.hpp:273
bool operator<(const TimeStamp &other) const
Comparison operator.
Definition: time_stamp.hpp:287
long long elapsed_micros(const TimeStamp &before) const
Calculate the time elapsed between two time stamps in microseconds.
Definition: time_stamp.hpp:135
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
Definition: time_stamp.hpp:100
double elapsed_now() const
Calculates the time elapsed between the time stamp and now.
Definition: time_stamp.hpp:121
long long elapsed_micros_now() const
Calculates the time elapsed between the time stamp and now in microseconds.
Definition: time_stamp.hpp:157
long long query_performance_frequency()
Wraps around the QueryPerformanceCounter function.
Definition: os_windows.cpp:33
long long query_performance_counter()
Wraps around the QueryPerformanceCounter function.
Definition: os_windows.cpp:25
FEAT namespace.
Definition: adjactor.hpp:12
TimeFormat
Definition: time_stamp.hpp:31