Last active
March 28, 2019 16:19
-
-
Save ffAudio/5e2523639caeb533149ce9694356893b to your computer and use it in GitHub Desktop.
JUCE ElapsedTime
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
/** | |
Stop watch to measure time intervals | |
\code{.cpp} | |
ElapsedTime timer; | |
// do stuff | |
DBG ("Doing stuff took: " << String (timer.elapsed()) << " milliseconds"); | |
timer.pause(); | |
// that doesn't count | |
timer.resume(); | |
\endcode | |
*/ | |
#pragma once | |
struct ElapsedTime | |
{ | |
ElapsedTime() = default; | |
void reset() | |
{ | |
startTime = Time::getMillisecondCounter(); | |
pauseTime = 0; | |
} | |
uint32 elapsed() const | |
{ | |
if (pauseTime > 0) | |
return pauseTime - startTime; | |
return Time::getMillisecondCounter() - startTime; | |
} | |
void pause() | |
{ | |
if (pauseTime == 0) | |
pauseTime = Time::getMillisecondCounter(); | |
} | |
void resume() | |
{ | |
if (pauseTime > 0) | |
{ | |
startTime += Time::getMillisecondCounter() - pauseTime; | |
pauseTime = 0; | |
} | |
} | |
private: | |
uint32 startTime { Time::getMillisecondCounter() }; | |
uint32 pauseTime = 0; | |
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ElapsedTime) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment