PCL
ElapsedTime.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/ElapsedTime.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_ElapsedTime_h
53 #define __PCL_ElapsedTime_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/String.h>
61 
62 #if !defined( __PCL_LINUX ) && !defined( __PCL_FREEBSD )
63 # include <pcl/Atomic.h>
64 # include <pcl/AutoLock.h>
65 # define PCL_CLOCK_ID 0
66 #endif
67 
68 #ifdef __PCL_LINUX
69 # include <time.h>
70 # ifdef CLOCK_MONOTONIC_RAW // since kernel 2.6.28
71 # define PCL_CLOCK_ID CLOCK_MONOTONIC_RAW
72 # else
73 # define PCL_CLOCK_ID CLOCK_MONOTONIC
74 # endif
75 #endif
76 
77 #ifdef __PCL_FREEBSD
78 # include <sys/timespec.h>
79 # ifdef CLOCK_MONOTONIC_PRECISE // since FreeBSD 10.1
80 # define PCL_CLOCK_ID CLOCK_MONOTONIC_PRECISE
81 # else
82 # define PCL_CLOCK_ID CLOCK_MONOTONIC
83 # endif
84 #endif
85 
86 #ifdef __PCL_MACOSX
87 # include <mach/mach.h>
88 # include <mach/mach_time.h>
89 #endif
90 
91 namespace pcl
92 {
93 
94 // ----------------------------------------------------------------------------
95 
130 class PCL_CLASS ElapsedTime
131 {
132 public:
133 
140  {
141  Reset();
142  }
143 
147  ElapsedTime( const ElapsedTime& ) = default;
148 
152  ElapsedTime& operator =( const ElapsedTime& ) = default;
153 
160  double operator ()() const
161  {
162  return TimeStamp() - m_start;
163  }
164 
169  void Reset()
170  {
171  m_start = TimeStamp();
172  }
173 
180  {
181  return ToIsoString( operator()() );
182  }
183 
189  String ToString() const
190  {
191  return ToString( operator()() );
192  }
193 
197  operator IsoString() const
198  {
199  return ToIsoString();
200  }
201 
205  operator String() const
206  {
207  return ToString();
208  }
209 
220  static double TimeStamp()
221  {
222 #if defined( __PCL_LINUX ) || defined( __PCL_FREEBSD )
223 
224  /*
225  * Linux/FreeBSD implementation based on clock_gettime().
226  */
227  timespec ts;
228  (void)clock_gettime( PCL_CLOCK_ID, &ts );
229  return ts.tv_sec + ts.tv_nsec*1.0e-09;
230 
231 #endif // __PCL_LINUX || __PCL_FREEBSD
232 
233 #ifdef __PCL_MACOSX
234 
235  /*
236  * macOS implementation based on Mach absolute time system services.
237  *
238  * https://developer.apple.com/library/mac/qa/qa1398/_index.html
239  * http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
240  */
241  static Mutex mutex;
242  static AtomicInt initialized;
243  static double absoluteToSeconds;
244  static uint64 offset;
245 
246  if ( !initialized )
247  {
248  volatile AutoLock lock( mutex );
249  if ( initialized.Load() == 0 )
250  {
251  mach_timebase_info_data_t timeBase = { 0 };
252  mach_timebase_info( &timeBase );
253  absoluteToSeconds = 1.0e-09*(double( timeBase.numer )/timeBase.denom);
254  offset = mach_absolute_time();
255  initialized.Store( 1 );
256  }
257  }
258 
259  uint64 t = mach_absolute_time();
260  return (t - offset)*absoluteToSeconds;
261 
262 #endif // __PCL_MACOSX
263 
264 #ifdef __PCL_WINDOWS
265 
266  /*
267  * Windows implementation based on Win32 performance counters.
268  *
269  * https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905%28v=vs.85%29.aspx
270  * https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904%28v=vs.85%29.aspx
271  * http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
272  * http://stackoverflow.com/questions/1676036/what-should-i-use-to-replace-gettimeofday-on-windows
273  */
274  static Mutex mutex;
275  static AtomicInt initialized;
276  static double countsToSeconds;
277  static uint64 offset;
278 
279  if ( !initialized )
280  {
281  volatile AutoLock lock( mutex );
282  if ( initialized.Load() == 0 )
283  {
284  uint64 performanceFrequency;
285  QueryPerformanceFrequency( (LARGE_INTEGER*)&performanceFrequency );
286  countsToSeconds = 1.0/performanceFrequency;
287  QueryPerformanceCounter( (LARGE_INTEGER*)&offset );
288  initialized.Store( 1 );
289  }
290  }
291 
292  uint64 t;
293  QueryPerformanceCounter( (LARGE_INTEGER*)&t );
294  return (t - offset)*countsToSeconds;
295 
296 #endif // __PCL_WINDOWS
297  }
298 
328  static IsoString ToIsoString( double seconds, int width = 0, int precision = 3 )
329  {
330  return ToString( seconds, width, precision, (IsoString*)0 );
331  }
332 
338  static String ToString( double s, int width = 0, int precision = 3 )
339  {
340  return ToString( s, width, precision, (String*)0 );
341  }
342 
350  friend double operator -( const ElapsedTime& t1, const ElapsedTime& t2 )
351  {
352  return t1.m_start - t2.m_start;
353  }
354 
355 private:
356 
357  double m_start; // timestamp in seconds
358 
359  template <class S>
360  static S ToString( double s, int w, int p, S* )
361  {
362  w = Max( 0, w );
363  p = Range( p, 0, 3 );
364 
365  if ( s < 60 )
366  {
367  if ( w > 0 )
368  if ( p > 0 )
369  w = Max( p+2, w+p+1 );
370 
371  const char* u;
372  if ( s < 0.001 )
373  {
374  s *= 1000000;
375  u = "us";
376  }
377  else if ( s < 1 )
378  {
379  s *= 1000;
380  u = "ms";
381  }
382  else
383  u = "s";
384 
385  if ( w > 0 )
386  return S().Format( "%*.*f %s", w, p, s, u );
387  return S().Format( "%.*f %s", p, s, u );
388  }
389 
390  int sign = (s < 0) ? -1 : +1;
391  s = Abs( s );
392 
393  if ( s < 3600 )
394  {
395  int m = TruncInt( s/60 );
396  s -= m*60;
397  p = Max( 0, p-1 );
398  return S().Format( "%02d:%0*.*f", m*sign, (p > 0) ? 3+p : 2, p, s );
399  }
400 
401  if ( s < 86400 )
402  {
403  int h = TruncInt( s/3600 );
404  s -= h*3600;
405  int m = TruncInt( s/60 );
406  s -= m*60;
407  p = Max( 0, p-2 );
408  return S().Format( "%02d:%02d:%0*.*f", h*sign, m, (p > 0) ? 3+p : 2, p, s );
409  }
410 
411  int d = TruncInt( s/86400 );
412  s -= d*86400;
413  int h = TruncInt( s/3600 );
414  s -= h*3600;
415  int m = TruncInt( s/60 );
416  s -= m*60;
417  return S().Format( "%d:%02d:%02d:%02d", d*sign, h, m, RoundInt( s ) );
418  }
419 };
420 
421 // ----------------------------------------------------------------------------
422 
423 } // pcl
424 
425 #endif // __PCL_ElapsedTime_h
426 
427 // ----------------------------------------------------------------------------
428 // EOF pcl/ElapsedTime.h - Released 2024-06-18T15:48:54Z
Atomic operations on integers.
Definition: Atomic.h:95
int Load()
Definition: Atomic.h:181
void Store(int newValue)
Definition: Atomic.h:194
Automatic mutex lock/unlock.
Definition: AutoLock.h:84
High-resolution time stamp.
Definition: ElapsedTime.h:131
ElapsedTime(const ElapsedTime &)=default
IsoString ToIsoString() const
Definition: ElapsedTime.h:179
static double TimeStamp()
Definition: ElapsedTime.h:220
static IsoString ToIsoString(double seconds, int width=0, int precision=3)
Definition: ElapsedTime.h:328
String ToString() const
Definition: ElapsedTime.h:189
static String ToString(double s, int width=0, int precision=3)
Definition: ElapsedTime.h:338
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
Adaptive mutual exclusion lock variable.
Definition: Mutex.h:209
Unicode (UTF-16) string.
Definition: String.h:8113
Complex< T1 > operator-(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:504
T Abs(const Complex< T > &c) noexcept
Definition: Complex.h:429
int RoundInt(T x) noexcept
Definition: Math.h:1503
int TruncInt(T x) noexcept
Definition: Math.h:1132
unsigned long long uint64
Definition: Defs.h:682
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
Definition: Utility.h:190
constexpr const T & Max(const T &a, const T &b) noexcept
Definition: Utility.h:119
PCL root namespace.
Definition: AbstractImage.h:77