a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
BasicTimer.hpp
Go to the documentation of this file.
1/*
2 * BasicTimer.hpp
3 *
4 * Created on: Oct 23, 2013
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 */
7
8#ifndef BASICTIMER_HPP_
9#define BASICTIMER_HPP_
10
11#include <cstdlib>
12#include <sys/time.h>
13
14namespace BOL
15{
16 // this is a class to do some basic timing stuff.
18 {
19 public:
20 static double
21 secondsSince( timeval const& referenceTimeval );
22 // this returns the number of seconds since referenceTimeval as a double.
23
24 BasicTimer( double givenDuration = 0.0,
25 timeval const* const startTimeval = NULL );
27
28 void
29 setStartTime( timeval const* const startTimeval = NULL );
30 // if startTimeval is provided, that becomes the new reference "time = 0".
31 // if a NULL pointer is provided, then the current time is taken and saved.
32 double
33 secondsSinceStart() const;
34 void
35 setDuration( double const givenDuration );
36 bool
37 withinDuration() const;
38 // this returns true if the current time is not later than givenDuration
39 // seconds after startTimeval.
40
41
42 protected:
44 // this is a given duration in seconds for whether it is that much later
45 // than the reference start time.
46 timeval startTimeval;
47 // this is the reference start time which time durations are calculated
48 // from.
49 };
50
51
52
53 inline double
54 BasicTimer::secondsSince( timeval const& referenceTimeval )
55 // this returns the number of seconds since referenceTimeval as a double.
56 {
57 timeval currentTimeval;
58 gettimeofday( &currentTimeval,
59 NULL );
60 return ( (double)( currentTimeval.tv_sec - referenceTimeval.tv_sec )
61 + 0.000001 * (double)( currentTimeval.tv_usec
62 - referenceTimeval.tv_usec ) );
63 }
64
65 inline void
66 BasicTimer::setStartTime( timeval const* const startTimeval )
67 // if startTimeval is provided, that becomes the new reference "time = 0".
68 // if a NULL pointer is provided, then the current time is taken and saved.
69 {
70 if( NULL == startTimeval )
71 {
72 gettimeofday( &(this->startTimeval),
73 NULL );
74 }
75 else
76 {
77 this->startTimeval = *startTimeval;
78 }
79 }
80
81 inline double
83 {
84 return secondsSince( startTimeval );
85 }
86
87 inline void
88 BasicTimer::setDuration( double const givenDuration )
89 {
90 this->givenDuration = givenDuration;
91 }
92
93 inline bool
95 // this returns true if the current time is not later than givenDuration
96 // seconds after startTimeval.
97 {
98 return ( givenDuration >= secondsSinceStart() );
99 }
100
101} /* namespace BOL */
102#endif /* BASICTIMER_HPP_ */
double secondsSinceStart() const
Definition: BasicTimer.hpp:82
void setDuration(double const givenDuration)
Definition: BasicTimer.hpp:88
bool withinDuration() const
Definition: BasicTimer.hpp:94
static double secondsSince(timeval const &referenceTimeval)
Definition: BasicTimer.hpp:54
void setStartTime(timeval const *const startTimeval=NULL)
Definition: BasicTimer.hpp:66
double givenDuration
Definition: BasicTimer.hpp:43
timeval startTimeval
Definition: BasicTimer.hpp:46
BasicTimer(double givenDuration=0.0, timeval const *const startTimeval=NULL)
Definition: BasicTimer.cpp:13