G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsStopwatch.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <ostream>
17
18// includes for wall clocks
19#if __cplusplus >= 201103L || _MSC_VER >= 1600
20# include <chrono>
21#elif defined(__linux__)
22# include <sys/time.h>
23#elif defined(_MSC_VER) || defined(__MINGW32__)
24# include <sys/timeb.h>
25#else
26# include <ctime>
27#endif
28
29// includes for CPU clocks
30#if defined(__linux__)
31# include <sys/resource.h>
32#else
33# include <ctime>
34#endif
35
36namespace gismo
37{
38
39inline std::ostream& formatTime(std::ostream& os, double sec)
40{
41 int flo = (int)(sec);
42 sec -= flo;
43 int hh = flo / 3600;
44 flo = flo % 3600;
45 int mm = flo / 60;
46 double ss = (flo % 60) + sec;
47 if (hh > 0) os << hh << "h ";
48 if (mm > 0) os << mm << "m ";
49 std::streamsize prec = os.precision();
50 os.precision(2);
51 os << ss << " s";
52 os.precision(prec);
53 return os;
54}
55
56
71template <typename Clock>
73{
74public:
75
77 gsGenericStopwatch() : m_value(0) { restart(); }
78
80 void restart() { m_start = Clock::getTime(); }
81
83 double stop() { return m_value = Clock::getTime() - m_start; }
84
86 double elapsed() const { return m_value; };
87
88 friend std::ostream& operator<< (std::ostream& os, const gsGenericStopwatch& sw)
89 { return formatTime(os, sw.m_value); }
90
91#if __cplusplus >= 201103L || _MSC_VER >= 1600 // C++11 //
92 gsGenericStopwatch (const gsGenericStopwatch&) = delete;
93 gsGenericStopwatch& operator=(const gsGenericStopwatch&) = delete;
94#endif
95
96private:
97 double m_start;
98 double m_value;
99}; // class gsGenericStopwatch
100
101
102/*
103 * SYSTEM-SPECIFIC WALL CLOCKS
104 */
105
106#if __cplusplus >= 201103L || _MSC_VER >= 1600 // C++11 //
107// highest-resolution wall clock time
108struct CXX11WallClock
109{
110 static double getTime()
111 {
112 return ((std::chrono::duration<double>)std::chrono::high_resolution_clock::now().time_since_epoch()).count();
113 }
114};
115
117typedef gsGenericStopwatch<CXX11WallClock> gsStopwatch;
118
119#elif defined(__linux__) // || defined(TARGET_OS_MAC) // LINUX //
120
121// higher resolution wall clock time
122struct LinuxWallClock
123{
124 static double getTime()
125 { timeval tv; gettimeofday(&tv, 0); return tv.tv_sec + 1e-6*tv.tv_usec; }
126};
127
129typedef gsGenericStopwatch<LinuxWallClock> gsStopwatch;
130
131#elif defined(_MSC_VER) || defined(__MINGW32__) // WINDOWS //
132
133struct WindowsWallClock
134{
135 static double getTime()
136 {
137 _timeb tb;
138 #ifdef __MINGW32__
139 _ftime( &tb );
140 #else
141 _ftime_s( &tb );
142 #endif
143 return (double)tb.time + tb.millitm / 1000.0;
144 }
145};
146
148typedef gsGenericStopwatch<WindowsWallClock> gsStopwatch;
149
150#else // PORTABLE //
151
152// simple, low-resolution (only integer seconds) wall clock time
153struct WallClock
154{
155 static double getTime()
156 { time_t secs; time (&secs); return secs; }
157};
158
161
162#endif
163
164
165/*
166 * SYSTEM-SPECIFIC CPU CLOCKS
167 */
168
169#if defined(__linux__) // || defined(TARGET_OS_MAC) // LINUX //
170
171// A non-portable, more expensive, but higher resolution CPU clock
172// which also does not suffer from the short wrap-around time of
173// CPUClock.
174struct LinuxCPUClock
175{
176 static double getTime()
177 {
178 rusage ru;
179 getrusage(RUSAGE_SELF, &ru);
180 return ru.ru_utime.tv_sec + 1.0e-6*ru.ru_utime.tv_usec;
181 }
182};
183
185typedef gsGenericStopwatch<LinuxCPUClock> gsCPUStopwatch;
186
187#else // PORTABLE //
188
189// A portable, but not very accurate CPU clock. Note that on a
190// typical Linux system, clock_t is just 4 bytes long and this will
191// wrap around after about 36 min.
192struct CPUClock
193{
194 static double getTime() { return (double) clock() / (double) CLOCKS_PER_SEC; }
195};
196
199
200#endif
201
202
203} // namespace gismo
A Stopwatch object can be used to measure execution time of code, algorithms, etc.
Definition gsStopwatch.h:73
double stop()
Return elapsed time in seconds.
Definition gsStopwatch.h:83
gsGenericStopwatch()
Declares a stop-watch.
Definition gsStopwatch.h:77
void restart()
Start taking the time.
Definition gsStopwatch.h:80
double elapsed() const
Returns the last recorded elapsed time.
Definition gsStopwatch.h:86
EIGEN_STRONG_INLINE tangent_expr< T > tv(const gsGeometryMap< T > &u)
The tangent boundary vector of a geometry map in 2D.
Definition gsExpressions.h:4513
The G+Smo namespace, containing all definitions for the library.
gsGenericStopwatch< WallClock > gsStopwatch
A stop-watch measuring real (wall) time.
Definition gsStopwatch.h:160
gsGenericStopwatch< CPUClock > gsCPUStopwatch
A stop-watch measuring CPU time.
Definition gsStopwatch.h:198