Skip to content

Instantly share code, notes, and snippets.

@ndvbd
Last active October 24, 2017 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndvbd/a4433937d5718cbe530c76fd104b8c89 to your computer and use it in GitHub Desktop.
Save ndvbd/a4433937d5718cbe530c76fd104b8c89 to your computer and use it in GitHub Desktop.
CPP StopWatch
// 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