-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.cpp
55 lines (44 loc) · 1.19 KB
/
Timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Author: [email protected] (Michael DiBernardo)
// Copyright Michael DiBernardo 2006
// Implementation of class Timer and related constructs.
#include "Timer.h"
#include <assert.h>
static const int kNumUSecsInMS = 1000;
static const int kNumUSecsInSec = 1000000;
Timer::Timer() : uSecsElapsed_(0), started_(false) {
}
Timer::~Timer() {
}
void Timer::start() {
assert(!started_);
gettimeofday(&lastTick_, NULL);
started_ = true;
}
void Timer::pause() {
assert(started_);
updateTime();
started_ = false;
}
void Timer::reset() {
if (started_ == true) updateTime();
uSecsElapsed_ = 0;
}
long Timer::getTimeInMilliseconds() const {
return (getTimeInMicroseconds() / kNumUSecsInMS);
}
long Timer::getTimeInMicroseconds() const {
if (started_) updateTime();
return uSecsElapsed_;
}
void Timer::updateTime() const {
long timePassedSinceLastTick = 0;
timeval currentTick;
gettimeofday(¤tTick, NULL);
timePassedSinceLastTick = (currentTick.tv_sec - lastTick_.tv_sec) * kNumUSecsInSec +
(currentTick.tv_usec - lastTick_.tv_usec);
uSecsElapsed_ += timePassedSinceLastTick;
lastTick_ = currentTick;
}
bool Timer::running() const {
return started_;
}