-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "timer.h" | ||
#include <time.h> | ||
|
||
namespace common | ||
{ | ||
|
||
Timer::Timer() | ||
{ | ||
restart(); | ||
} | ||
|
||
Timer Timer::restart() | ||
{ | ||
Timer tmp = *this; | ||
clock_gettime( CLOCK_MONOTONIC_RAW, this ); | ||
return tmp; | ||
} | ||
|
||
|
||
Timer Timer::operator-(const Timer & a) | ||
{ | ||
Timer t(*this); | ||
return t -= a; | ||
} | ||
|
||
Timer & Timer::operator-=(const Timer & a) | ||
{ | ||
tv_sec -= a.tv_sec; | ||
tv_nsec -= a.tv_nsec; | ||
if(tv_nsec < 0) | ||
{ | ||
--tv_sec; | ||
tv_nsec += 1e9; | ||
} | ||
return *this; | ||
} | ||
|
||
Timer Timer::operator+(const Timer & a) | ||
{ | ||
Timer t(*this); | ||
return t += a; | ||
} | ||
|
||
Timer & Timer::operator+=(const Timer & a) | ||
{ | ||
tv_sec += a.tv_sec; | ||
tv_nsec += a.tv_nsec; | ||
if(tv_nsec < 1e9) | ||
{ | ||
++tv_sec; | ||
tv_nsec -= 1e9; | ||
} | ||
return *this; | ||
} | ||
|
||
|
||
} // namespace common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef TIMER_H | ||
#define TIMER_H | ||
|
||
#include <time.h> | ||
|
||
namespace common | ||
{ | ||
|
||
class Timer : public timespec | ||
{ | ||
public: | ||
Timer(); | ||
Timer restart(); | ||
|
||
Timer operator-(const Timer & a); | ||
Timer & operator-=(const Timer & a); | ||
Timer operator+(const Timer & a); | ||
Timer & operator+=(const Timer & a); | ||
|
||
double seconds() { | ||
return tv_sec + (double)tv_nsec/1e9; | ||
}; | ||
long unsigned us() { | ||
return tv_sec*1e9 + tv_nsec; | ||
}; | ||
}; | ||
|
||
} // namespace common | ||
|
||
#endif // TIMER_H |