Last active
October 24, 2017 08:27
-
-
Save ndvbd/a4433937d5718cbe530c76fd104b8c89 to your computer and use it in GitHub Desktop.
CPP StopWatch
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
// Written by Nadav Benedek 2017 | |
class StopWatch { | |
private: | |
double accumulatedTimeSeconds = 0; | |
double startTime = 0; | |
enum State {NONE, START, PAUSE, RESUME, STOP}; | |
State state; | |
public: | |
StopWatch() : accumulatedTimeSeconds(0), startTime(0), state(NONE) { | |
} | |
// Resets the StopWatch Completely. | |
void start() { | |
startTime = (double)getTickCount(); | |
accumulatedTimeSeconds = 0; | |
state = START; | |
} | |
void pause() { | |
assert(state == START || state == RESUME); | |
accumulatedTimeSeconds += ((double)getTickCount() - startTime) / (getTickFrequency()); | |
state = PAUSE; | |
} | |
void resume() { | |
assert(state == PAUSE); | |
startTime = (double)getTickCount(); | |
state = RESUME; | |
} | |
double stop() { | |
assert(state == START || state == RESUME); | |
accumulatedTimeSeconds += ((double)getTickCount() - startTime) / (getTickFrequency()); | |
state = STOP; | |
return accumulatedTimeSeconds; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment