Skip to content

Instantly share code, notes, and snippets.

@ffAudio
Last active March 28, 2019 16:19
Show Gist options
  • Save ffAudio/5e2523639caeb533149ce9694356893b to your computer and use it in GitHub Desktop.
Save ffAudio/5e2523639caeb533149ce9694356893b to your computer and use it in GitHub Desktop.
JUCE ElapsedTime
/**
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