n2p2 - A neural network potential package
nnp::Stopwatch Class Reference

Implements a simple stopwatch on different platforms. More...

#include <Stopwatch.h>

Collaboration diagram for nnp::Stopwatch:

Public Member Functions

 Stopwatch ()
 
void start (bool newLoop=true)
 Start the stopwatch. More...
 
double stop ()
 Stop and return total time. More...
 
double loop ()
 Stop and return loop time. More...
 
double getTotal () const
 Return total time elapsed (of a stopped watch). More...
 
double getLoop () const
 Return time elapsed in last loop interval (of a stopped watch). More...
 
void reset ()
 Reset stopwatch (total and loop time zero, clock not running). More...
 

Private Types

enum  State { STOPPED , RUNNING }
 

Private Member Functions

void stopTime ()
 
double updateTime ()
 

Private Attributes

State state
 
bool resetLoop
 
double timeTotal
 
double timeLoop
 

Static Private Attributes

static const double NSEC = 1E-9
 

Detailed Description

Implements a simple stopwatch on different platforms.

Definition at line 38 of file Stopwatch.h.

Member Enumeration Documentation

◆ State

enum nnp::Stopwatch::State
private
Enumerator
STOPPED 
RUNNING 

Definition at line 63 of file Stopwatch.h.

64 {
65 STOPPED,
67 };

Constructor & Destructor Documentation

◆ Stopwatch()

Stopwatch::Stopwatch ( )

Definition at line 24 of file Stopwatch.cpp.

25{
26 state = STOPPED;
27 timeTotal = 0.0;
28 timeLoop = 0.0;
29#ifdef N2P2_NO_TIME
30 //fprintf(stderr, "WARNING: Stopwatch is using dummy implementation.\n");
31#elif __linux__
32 time.tv_sec = 0;
33 time.tv_nsec = 0;
34#elif __MACH__
35 time = 0;
36#endif
37
38}
double timeLoop
Definition: Stopwatch.h:73
double timeTotal
Definition: Stopwatch.h:72

References state, STOPPED, timeLoop, and timeTotal.

Member Function Documentation

◆ start()

void Stopwatch::start ( bool  newLoop = true)

Start the stopwatch.

Parameters
[in]resetLoopOptional. If true reset loop time, i.e. this start() call is then also the beginning of a new loop. Use false if you want to accumulate further time in the loop timer. Default: true.

Definition at line 40 of file Stopwatch.cpp.

41{
42 if (state == STOPPED)
43 {
44#ifdef N2P2_NO_TIME
45#elif __linux__
46 clock_gettime(CLOCK_MONOTONIC, &time);
47#elif __MACH__
48 time = mach_absolute_time();
49#endif
50 if (newLoop) timeLoop = 0.0;
51 state = RUNNING;
52 }
53 else
54 {
55 fprintf(stderr,
56 "WARNING: Unable to start clock, clock already running.\n");
57 }
58
59 return;
60}

References RUNNING, state, STOPPED, and timeLoop.

Referenced by runTest().

Here is the caller graph for this function:

◆ stop()

double Stopwatch::stop ( )

Stop and return total time.

Definition at line 62 of file Stopwatch.cpp.

63{
64 stopTime();
65
66 return getTotal();
67}
double getTotal() const
Return total time elapsed (of a stopped watch).
Definition: Stopwatch.h:56
void stopTime()
Definition: Stopwatch.cpp:92

References getTotal(), and stopTime().

Referenced by runTest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loop()

double Stopwatch::loop ( )

Stop and return loop time.

Definition at line 69 of file Stopwatch.cpp.

70{
71 stopTime();
72
73 return getLoop();
74}
double getLoop() const
Return time elapsed in last loop interval (of a stopped watch).
Definition: Stopwatch.h:58

References getLoop(), and stopTime().

Here is the call graph for this function:

◆ getTotal()

double nnp::Stopwatch::getTotal ( ) const
inline

Return total time elapsed (of a stopped watch).

Definition at line 56 of file Stopwatch.h.

56{return timeTotal;};

References timeTotal.

Referenced by stop().

Here is the caller graph for this function:

◆ getLoop()

double nnp::Stopwatch::getLoop ( ) const
inline

Return time elapsed in last loop interval (of a stopped watch).

Definition at line 58 of file Stopwatch.h.

58{return timeLoop;};

References timeLoop.

Referenced by loop().

Here is the caller graph for this function:

◆ reset()

void Stopwatch::reset ( )

Reset stopwatch (total and loop time zero, clock not running).

Definition at line 76 of file Stopwatch.cpp.

77{
78 state = STOPPED;
79 timeTotal = 0.0;
80 timeLoop = 0.0;
81#ifdef N2P2_NO_TIME
82#elif __linux__
83 time.tv_sec = 0;
84 time.tv_nsec = 0;
85#elif __MACH__
86 time = 0;
87#endif
88
89 return;
90}

References state, STOPPED, timeLoop, and timeTotal.

Referenced by runTest().

Here is the caller graph for this function:

◆ stopTime()

void Stopwatch::stopTime ( )
private

Definition at line 92 of file Stopwatch.cpp.

93{
94 double timeInterval = updateTime();
95 timeLoop += timeInterval;
96 timeTotal += timeInterval;
97 state = STOPPED;
98
99 return;
100}
double updateTime()
Definition: Stopwatch.cpp:102

References state, STOPPED, timeLoop, timeTotal, and updateTime().

Referenced by loop(), and stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateTime()

double Stopwatch::updateTime ( )
private

Definition at line 102 of file Stopwatch.cpp.

103{
104 if (state == RUNNING)
105 {
106#ifdef N2P2_NO_TIME
107 return 1.0;
108#elif __linux__
109 time_t secLast = time.tv_sec;
110 long nsecLast = time.tv_nsec;
111
112 clock_gettime(CLOCK_MONOTONIC, &time);
113 return (double)(time.tv_sec - secLast)
114 + (double)(time.tv_nsec - nsecLast) * NSEC;
115#elif __MACH__
116 uint64_t lastTime = time;
117 uint64_t time = mach_absolute_time();
118 static mach_timebase_info_data_t info = {0, 0};
119
120 if (info.denom == 0) mach_timebase_info(&info);
121 return ((time - lastTime) * (info.numer / info.denom)) * NSEC;
122#endif
123 }
124 else
125 {
126 fprintf(stderr,
127 "WARNING: Unable to update time, clock not running.\n");
128 return 0.0;
129 }
130}
static const double NSEC
Definition: Stopwatch.h:71

References NSEC, RUNNING, and state.

Referenced by stopTime().

Here is the caller graph for this function:

Member Data Documentation

◆ state

State nnp::Stopwatch::state
private

Definition at line 69 of file Stopwatch.h.

Referenced by reset(), start(), stopTime(), Stopwatch(), and updateTime().

◆ resetLoop

bool nnp::Stopwatch::resetLoop
private

Definition at line 70 of file Stopwatch.h.

◆ NSEC

const double Stopwatch::NSEC = 1E-9
staticprivate

Definition at line 71 of file Stopwatch.h.

Referenced by updateTime().

◆ timeTotal

double nnp::Stopwatch::timeTotal
private

Definition at line 72 of file Stopwatch.h.

Referenced by getTotal(), reset(), stopTime(), and Stopwatch().

◆ timeLoop

double nnp::Stopwatch::timeLoop
private

Definition at line 73 of file Stopwatch.h.

Referenced by getLoop(), reset(), start(), stopTime(), and Stopwatch().


The documentation for this class was generated from the following files: