Skip to content

Instantly share code, notes, and snippets.

@superwills
Created March 22, 2024 02:08
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 superwills/72f040e0ed3553ff197cfefbc0eb5ea1 to your computer and use it in GitHub Desktop.
Save superwills/72f040e0ed3553ff197cfefbc0eb5ea1 to your computer and use it in GitHub Desktop.
StopWatch / Every
#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