diff --git a/Stopwatch.hpp b/Stopwatch.hpp
index 3322a20..c5a6153 100644
--- a/Stopwatch.hpp
+++ b/Stopwatch.hpp
@@ -1,10 +1,10 @@
//MyClasses/Stopwatch.hpp
//Matthew Ellison
// Created: 10-30-2018
-//Modified: 12-10-2018
-//This file creates a class that can be used on many programs to time them
+//Modified: 02-07-2019
+//This file defines a class that can be used as a simple timer for programs
/*
- Copyright (C) 2018 Matthew Ellison
+ 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
@@ -20,82 +20,107 @@
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;
- std::chrono::high_resolution_clock::time_point endTime;
- bool hasStarted;
- bool hasEnded;
+ 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(){
- hasStarted = hasEnded = false;
+ //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();
- hasStarted = true;
}
+ //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();
- hasEnded = true;
+ 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(hasStarted && hasEnded){
+ //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 0;
- }
- }
- double getSeconds(){
- if(hasStarted && hasEnded){
- std::chrono::duration dur = endTime - startTime;
- return dur.count();
- }
- else{
- return 0;
- }
- }
- double getMilli(){
- if(hasStarted && hasEnded){
- std::chrono::duration dur = endTime - startTime;
- return dur.count();
- }
- else{
- return 0;
- }
- }
- double getMicro(){
- if(hasStarted && hasEnded){
- std::chrono::duration dur = endTime - startTime;
- return dur.count();
- }
- else{
- return 0;
+ return -1;
}
}
+ //Return the duration in nanoseconds
double getNano(){
- if(hasStarted && hasEnded){
+ //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 0;
+ return -1;
}
}
- void reset(){
- hasStarted = hasEnded = false;
- endTime = startTime = std::chrono::high_resolution_clock::time_point();
+ //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