Created
March 22, 2024 02:08
-
-
Save superwills/72f040e0ed3553ff197cfefbc0eb5ea1 to your computer and use it in GitHub Desktop.
StopWatch / Every
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
#pragma once | |
#include <chrono> | |
struct StopWatch { | |
std::chrono::high_resolution_clock::time_point start; | |
StopWatch() { | |
reset(); | |
} | |
void reset() { | |
start = std::chrono::high_resolution_clock::now(); | |
} | |
unsigned long long milli() const { | |
std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now(); | |
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); | |
return ms.count(); | |
} | |
double sec() const { | |
std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now(); | |
return std::chrono::duration<double>(stop - start).count(); | |
} | |
}; | |
struct Every { | |
StopWatch watch; | |
double interval = 0, last = 0; | |
Every( double iInterval ) : interval( iInterval ) { | |
} | |
bool isTime() { | |
double now = watch.sec(); | |
double diff = now - last; | |
bool istime = 0; | |
if( diff > interval ) { | |
istime = true; | |
last = now; | |
} | |
return istime; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment