mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
144 lines
5.0 KiB
C++
144 lines
5.0 KiB
C++
//MyClasses/Stopwatch.hpp
|
|
//Matthew Ellison
|
|
// Created: 10-30-2018
|
|
//Modified: 02-07-2019
|
|
//This file defines a class that can be used as a simple timer for programs
|
|
/*
|
|
Copyright (C) 2019 Matthew Ellison
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
#ifndef STOPWATCH_HPP
|
|
#define STOPWATCH_HPP
|
|
|
|
|
|
#include <chrono>
|
|
|
|
namespace mee{
|
|
class Stopwatch{
|
|
private:
|
|
std::chrono::high_resolution_clock::time_point startTime; //The time the start 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 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
|
|
if(timeResolution == HOUR){
|
|
std::chrono::duration<double, std::ratio<3600LL>> dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
else if(timeResolution == MINUTE){
|
|
std::chrono::duration<double, std::ratio<60LL>> dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
else if(timeResolution == SECOND){
|
|
std::chrono::duration<double> dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
else if(timeResolution == MILLISECOND){
|
|
std::chrono::duration<double, std::milli> dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
else if(timeResolution == MICROSECOND){
|
|
std::chrono::duration<double, std::micro> dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
else if(timeResolution == NANOSECOND){
|
|
std::chrono::duration<double, std::nano> dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
else if(timeResolution == DEFAULT){
|
|
std::chrono::high_resolution_clock::duration dur = (endTime - startTime);
|
|
timePassed = dur.count();
|
|
}
|
|
|
|
return timePassed;
|
|
}
|
|
public:
|
|
Stopwatch(){
|
|
//Make sure the flags are set to false to show nothing has been called yet
|
|
hasStarted = hasStopped = false;
|
|
startTime = endTime = std::chrono::high_resolution_clock::time_point(); //Set the times with a blank time
|
|
}
|
|
~Stopwatch(){
|
|
|
|
}
|
|
//Set the start time and flag and make sure the stop flag is unset
|
|
void start(){
|
|
hasStarted = true; //Show that the stopwatch has been started
|
|
hasStopped = false; //Show that the stopwatch is still running. Security in case the Stopwatch is used in multiple places
|
|
//Put this last to ensure that the time recorded is as close to the return time as possible
|
|
startTime = std::chrono::high_resolution_clock::now();
|
|
}
|
|
//Set the stop time and flag
|
|
void stop(){
|
|
//Put this first to ensure the time recorded is as close to the call time as possible
|
|
endTime = std::chrono::high_resolution_clock::now();
|
|
//Make sure the stopwatch has started before you say it has stopped
|
|
if(hasStarted){
|
|
hasStopped = true; //Show that the stop function has been called
|
|
}
|
|
}
|
|
//Return the duration in nanoseconds
|
|
double getNano(){
|
|
return getTime(NANOSECOND);
|
|
}
|
|
//Return the duration in microseconds
|
|
double getMicro(){
|
|
return getTime(MICROSECOND);
|
|
}
|
|
//Return the duration in milliseconds
|
|
double getMilli(){
|
|
return getTime(MILLISECOND);
|
|
}
|
|
//Return the duration in seconds
|
|
double getSeconds(){
|
|
return getTime(SECOND);
|
|
}
|
|
//Return the duration in minutes
|
|
double getMinutes(){
|
|
return getTime(MINUTE);
|
|
}
|
|
//Return the duration in hours
|
|
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
|
|
void reset(){
|
|
hasStarted = hasStopped = false; //Set the flags as though nothing has happened
|
|
endTime = startTime = std::chrono::high_resolution_clock::time_point(); //Set the times with a blank time
|
|
}
|
|
}; //end class Stopwatch
|
|
} //end namespace mee
|
|
|
|
#endif //end STOPWATCH_HPP
|