//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 . */ #ifndef STOPWATCH_HPP #define STOPWATCH_HPP #include 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 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(); hasStopped = true; //Show that the stop function has been called } //Return the duration in the default time period for the high_resolution_clock 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 double getNano(){ //If the stopwatch has started and stopped return the difference in the 2 times in nanoseconds if(hasStarted && hasStopped){ std::chrono::duration dur = endTime - startTime; return dur.count(); } //Otherwise return -1 as an error message else{ return -1; } } //Return the duration in microseconds double getMicro(){ //If the stopwatch has started and stopped return the difference in the 2 times in microseconds if(hasStarted && hasStopped){ std::chrono::duration dur = endTime - startTime; return dur.count(); } //Otherwise return -1 as an error message else{ return -1; } } //Return the duration in milliseconds double getMilli(){ //If the stopwatch has started and stopped return the difference in the 2 times in milliseconds if(hasStarted && hasStopped){ std::chrono::duration dur = endTime - startTime; return dur.count(); } //Otherwise return -1 as an error message else{ return -1; } } //Return the duration in seconds double getSeconds(){ //If the stopwatch has started and stopped return the difference in the 2 times in seconds if(hasStarted && hasStopped){ std::chrono::duration dur = endTime - startTime; return dur.count(); } //Otherwise return -1 as an error message else{ return -1; } } //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