mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Reworked get(Time) function to for easier upkeep
This commit is contained in:
@@ -34,6 +34,38 @@ private:
|
|||||||
std::chrono::high_resolution_clock::time_point endTime; //The time the stop function was called
|
std::chrono::high_resolution_clock::time_point endTime; //The time the stop function was called
|
||||||
bool hasStarted; //A flag to show that start() has been called
|
bool hasStarted; //A flag to show that start() has been called
|
||||||
bool hasStopped; //A flag to show that stop() has been called
|
bool hasStopped; //A flag to show that stop() has been called
|
||||||
|
enum TIME_RESOLUTION {HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND, DEFAULT};
|
||||||
|
//Return the duration in the default time period for the high_resolution_clock
|
||||||
|
double getTime(TIME_RESOLUTION timeResolution){
|
||||||
|
double timePassed = 0; //Holds the amount of time that has passed
|
||||||
|
//If the timer hasn't been stopped then record the time right now. This will simulate looping at the stopwatch while it is still running
|
||||||
|
//I put this at the beginning to get the timestamp at close to the calling of the function as possible
|
||||||
|
if(!hasStopped){
|
||||||
|
endTime = std::chrono::high_resolution_clock::now();
|
||||||
|
}
|
||||||
|
//If the timer hasn't been started return a -1 as an error
|
||||||
|
if(!hasStarted){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
//Decide what resolution to make the duration
|
||||||
|
switch(timeResolution){
|
||||||
|
case HOUR: std::chrono::duration<double, std::ratio<3600LL>> dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
case MINUTE: std::chrono::duration<double, std::ratio<60LL>> dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
case SECOND: std::chrono::duration<double> dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
case MILLISECOND: std::chrono::duration<double, std::milli> dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
case MICROSECOND: std::chrono::duration<double, std::micro> dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
case NANOSECOND: std::chrono::duration<double, std::nano> dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
case DEFAULT: std::chrono::high_resolution_clock::duration dur = (endTime - startTime);
|
||||||
|
timePassed = dur.count(); break;
|
||||||
|
}
|
||||||
|
return timePassed;
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
Stopwatch(){
|
Stopwatch(){
|
||||||
//Make sure the flags are set to false to show nothing has been called yet
|
//Make sure the flags are set to false to show nothing has been called yet
|
||||||
@@ -54,66 +86,38 @@ public:
|
|||||||
void stop(){
|
void stop(){
|
||||||
//Put this first to ensure the time recorded is as close to the call time as possible
|
//Put this first to ensure the time recorded is as close to the call time as possible
|
||||||
endTime = std::chrono::high_resolution_clock::now();
|
endTime = std::chrono::high_resolution_clock::now();
|
||||||
hasStopped = true; //Show that the stop function has been called
|
//Make sure the stopwatch has started before you say it has stopped
|
||||||
}
|
if(hasStarted){
|
||||||
//Return the duration in the default time period for the high_resolution_clock
|
hasStopped = true; //Show that the stop function has been called
|
||||||
double getTime(){
|
|
||||||
//If the stopwatch has started and stopped return the difference in the 2 times as the duration
|
|
||||||
if(hasStarted && hasStopped){
|
|
||||||
return (endTime - startTime).count();
|
|
||||||
}
|
|
||||||
//Otherwise return -1 as an error message
|
|
||||||
else{
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Return the duration in nanoseconds
|
//Return the duration in nanoseconds
|
||||||
double getNano(){
|
double getNano(){
|
||||||
//If the stopwatch has started and stopped return the difference in the 2 times in nanoseconds
|
return getTime(NANOSECOND);
|
||||||
if(hasStarted && hasStopped){
|
|
||||||
std::chrono::duration<double, std::nano> dur = endTime - startTime;
|
|
||||||
return dur.count();
|
|
||||||
}
|
|
||||||
//Otherwise return -1 as an error message
|
|
||||||
else{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Return the duration in microseconds
|
//Return the duration in microseconds
|
||||||
double getMicro(){
|
double getMicro(){
|
||||||
//If the stopwatch has started and stopped return the difference in the 2 times in microseconds
|
return getTime(MICROSECOND);
|
||||||
if(hasStarted && hasStopped){
|
|
||||||
std::chrono::duration<double, std::micro> dur = endTime - startTime;
|
|
||||||
return dur.count();
|
|
||||||
}
|
|
||||||
//Otherwise return -1 as an error message
|
|
||||||
else{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Return the duration in milliseconds
|
//Return the duration in milliseconds
|
||||||
double getMilli(){
|
double getMilli(){
|
||||||
//If the stopwatch has started and stopped return the difference in the 2 times in milliseconds
|
return getTime(MILLISECOND);
|
||||||
if(hasStarted && hasStopped){
|
|
||||||
std::chrono::duration<double, std::milli> dur = endTime - startTime;
|
|
||||||
return dur.count();
|
|
||||||
}
|
|
||||||
//Otherwise return -1 as an error message
|
|
||||||
else{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Return the duration in seconds
|
//Return the duration in seconds
|
||||||
double getSeconds(){
|
double getSeconds(){
|
||||||
//If the stopwatch has started and stopped return the difference in the 2 times in seconds
|
return getTime(SECOND);
|
||||||
if(hasStarted && hasStopped){
|
}
|
||||||
std::chrono::duration<double> dur = endTime - startTime;
|
//Return the duration in minutes
|
||||||
return dur.count();
|
double getMinutes(){
|
||||||
}
|
return getTime(MINUTE);
|
||||||
//Otherwise return -1 as an error message
|
}
|
||||||
else{
|
//Return the duration in hours
|
||||||
return -1;
|
double getHours(){
|
||||||
}
|
return getTime(HOUR);
|
||||||
|
}
|
||||||
|
//Return the duration in the default resolution of high_resolution_clock
|
||||||
|
double getTime(){
|
||||||
|
return getTime(DEFAULT);
|
||||||
}
|
}
|
||||||
//This function resets all the variables so that it can be run again
|
//This function resets all the variables so that it can be run again
|
||||||
void reset(){
|
void reset(){
|
||||||
|
|||||||
Reference in New Issue
Block a user