-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
63 lines (51 loc) · 1.94 KB
/
Timer.h
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
56
57
58
59
60
61
62
63
// Author: [email protected] (Michael DiBernardo)
// Copyright Michael DiBernardo 2006
//
// A simple low resolution timer. The current implementation uses
// gettimeofday() to compute elapsed time, so the granularity of the timer
// depends on the granularity of that call. See 'man gettimeofday' for more
// information.
#ifndef __TIMER_H__
#define __TIMER_H__
#include <sys/time.h>
#include "Basics.h"
class Timer {
public:
// Create a new timer. A timer starts in the 'paused' state.
Timer();
// Free resources used by this timer.
~Timer();
// Start the timer running. It is an error to call 'start' on an
// already-running timer: This will cause an assertion to fail.
void start();
// Pause a running timer. It is an error to call 'pause' on an already-paused
// timer: This will cause an assertion to fail.
void pause();
// Reset the timer to 0. You can call this at any time: A running timer will
// start over at 0 and immediately begin recording time, whereas a paused
// timer will just start at 0.
void reset();
// Get the time elapsed from the first 'start' call after the most recent
// reset (or instantiation) in milliseconds.
long getTimeInMilliseconds() const;
// Get the time elapsed from the first 'start' call after the most recent
// reset (or instantiation) in microseconds.
long getTimeInMicroseconds() const;
// Is this timer running?
bool running() const;
private:
// The last 'gettimeofday' call result. This is mutable because it is updated
// when 'getTimeIn*' is called.
// TODO: This is completely unnecessary, refactor so that mutations are only
// done on reset.
mutable timeval lastTick_;
// The total recorded number of microseconds that have elapsed since the last
// reset.
mutable long uSecsElapsed_;
// Is this timer running?
bool started_;
// Helper function to update the elapsed time.
void updateTime() const;
DISALLOW_EVIL_CONSTRUCTORS(Timer);
};
#endif