AJA NTV2 SDK  17.0.1.1246
NTV2 SDK 17.0.1.1246
timer.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 #include "ajabase/common/timer.h"
11 #include "ajabase/system/system.h"
12 #include <iomanip>
13 
14 
16  : mPrecision (inPrecision)
17 {
18  Reset();
19 }
20 
21 
22 void AJATimer::Start (void)
23 {
24  // Save the time at start
25  switch (mPrecision)
26  {
27  default:
31  }
32  mRun = true;
33 }
34 
35 
36 void AJATimer::Stop (void)
37 {
38  // Save the time at stop...
39  switch (mPrecision)
40  {
41  default:
45  }
46  mRun = false;
47 }
48 
49 
50 void AJATimer::Reset (void)
51 {
52  // Clear the start and stop time
53  mStartTime = mStopTime = 0;
54  mRun = false;
55 }
56 
57 
58 uint32_t AJATimer::ElapsedTime (void) const
59 {
60  if (IsRunning()) // Running:
61  switch (mPrecision)
62  {
63  default:
64  case AJATimerPrecisionMilliseconds: return uint32_t(AJATime::GetSystemMilliseconds() - mStartTime);
65  case AJATimerPrecisionMicroseconds: return uint32_t(AJATime::GetSystemMicroseconds() - mStartTime);
66  case AJATimerPrecisionNanoseconds: return uint32_t(AJATime::GetSystemNanoseconds() - mStartTime);
67  }
68  // Stopped:
69  return uint32_t(mStopTime - mStartTime);
70 }
71 
72 
73 #define __fDEC(__x__,__w__,__p__) std::dec << std::fixed << std::setw(__w__) << std::setprecision(__p__) << (__x__)
74 
75 std::ostream & AJATimer::Print (std::ostream & oss) const
76 {
77  const double secs(ETSecs());
78  if (secs > 60.0)
79  {
80  const double mins (secs / 60.0);
81  if (mins > 60.0)
82  {
83  const double hrs (mins / 60.0);
84  if (hrs > 24.0)
85  {
86  const double days (hrs / 24.0);
87  oss << __fDEC(days,4,1) << " days";
88  }
89  else
90  oss << __fDEC(hrs,4,1) << " hrs";
91  }
92  else
93  oss << __fDEC(mins,4,1) << " mins";
94  }
95  else if (secs >= 1.0)
96  oss << __fDEC(secs,4,1) << " secs";
97  else if (secs >= 1.0E-03)
98  oss << __fDEC(secs*1.0E+3,4,1) << " msec";
99  else if (secs >= 1.0E-06)
100  oss << __fDEC(secs*1.0E+6,4,1) << " usec";
101  else if (secs >= 1.0E-09)
102  oss << __fDEC(secs*1.0E+9,4,1) << " nsec";
103  else if (secs >= 1.0E-12)
104  oss << __fDEC(secs*1.0E+12,4,1) << " psec";
105  else
106  oss << "0 sec";
107  return oss;
108 }
109 
110 
111 std::string AJATimer::PrecisionName (const AJATimerPrecision precision, const bool longName) // STATIC
112 {
113  if (longName)
114  switch (precision)
115  {
116  default:
117  case AJATimerPrecisionMilliseconds: return "milliseconds";
118  case AJATimerPrecisionMicroseconds: return "microseconds";
119  case AJATimerPrecisionNanoseconds: return "nanoseconds";
120  }
121  else
122  switch (precision)
123  {
124  default:
125  case AJATimerPrecisionMilliseconds: return "ms";
126  case AJATimerPrecisionMicroseconds: return "us";
127  case AJATimerPrecisionNanoseconds: return "ns";
128  }
129 }
130 
131 double AJATimer::PrecisionSecs (const AJATimerPrecision precision) // STATIC
132 {
133  switch (precision)
134  {
135  default:
136  case AJATimerPrecisionMilliseconds: return 1.0E-03;
137  case AJATimerPrecisionMicroseconds: return 1.0E-06;
138  case AJATimerPrecisionNanoseconds: return 1.0E-09;
139  }
140 }
AJATimer::IsRunning
bool IsRunning(void) const
Definition: timer.h:81
AJATimer::AJATimer
AJATimer(const AJATimerPrecision precision=AJATimerPrecisionMilliseconds)
Definition: timer.cpp:15
AJATimerPrecision
AJATimerPrecision
Definition: timer.h:14
systemtime.h
Declares the AJATime class.
AJATime::GetSystemMicroseconds
static uint64_t GetSystemMicroseconds(void)
Returns the current value of the host's high-resolution clock, in microseconds.
Definition: systemtime.cpp:221
AJATimer::Print
std::ostream & Print(std::ostream &oss) const
Definition: timer.cpp:75
timer.h
Declares the AJATimer class.
AJATimer::ETSecs
double ETSecs(void) const
Definition: timer.h:64
AJATimer::Stop
void Stop(void)
Definition: timer.cpp:36
AJATime::GetSystemNanoseconds
static uint64_t GetSystemNanoseconds(void)
Returns the current value of the host's high-resolution clock, in nanoseconds.
Definition: systemtime.cpp:236
system.h
System specific functions.
common.h
Private include file for all ajabase sources.
AJATimer::PrecisionName
static std::string PrecisionName(const AJATimerPrecision precision, const bool longName=true)
Definition: timer.cpp:111
__fDEC
#define __fDEC(__x__, __w__, __p__)
Definition: timer.cpp:73
AJATimer::PrecisionSecs
static double PrecisionSecs(const AJATimerPrecision precision)
Definition: timer.cpp:131
AJATimerPrecisionMicroseconds
@ AJATimerPrecisionMicroseconds
Definition: timer.h:17
AJATimerPrecisionNanoseconds
@ AJATimerPrecisionNanoseconds
Definition: timer.h:18
AJATimer::ElapsedTime
uint32_t ElapsedTime(void) const
Definition: timer.cpp:58
AJATimer::Start
void Start(void)
Definition: timer.cpp:22
AJATimerPrecisionMilliseconds
@ AJATimerPrecisionMilliseconds
Definition: timer.h:16
AJATime::GetSystemMilliseconds
static uint64_t GetSystemMilliseconds(void)
Returns the current value of the host's high-resolution clock, in milliseconds.
Definition: systemtime.cpp:206
AJATimer::Reset
void Reset(void)
Definition: timer.cpp:50