diff --git a/Stopwatch.hpp b/Stopwatch.hpp new file mode 100644 index 0000000..1649b76 --- /dev/null +++ b/Stopwatch.hpp @@ -0,0 +1,80 @@ +//MyClasses/Stopwatch.hpp +//Matthew Ellison +// Created: 10-30-2018 +//Modified: 10-30-2018 +//This file creates a class that can be used on many programs to time them + + +#include + + +class Stopwatch{ +private: + std::chrono::high_resolution_clock::time_point startTime; + std::chrono::high_resolution_clock::time_point endTime; + bool hasStarted; + bool hasEnded; +public: + Stopwatch(){ + hasStarted = hasEnded = false; + } + ~Stopwatch(){ + + } + void start(){ + startTime = std::chrono::high_resolution_clock::now(); + hasStarted = true; + } + void stop(){ + endTime = std::chrono::high_resolution_clock::now(); + hasEnded = true; + } + double getTime(){ + if(hasStarted && hasEnded){ + return (endTime - startTime).count(); + } + 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; + } + } + double getNano(){ + if(hasStarted && hasEnded){ + std::chrono::duration dur = endTime - startTime; + return dur.count(); + } + else{ + return 0; + } + } + void reset(){ + hasStarted = hasEnded = false; + endTime = startTime = std::chrono::high_resolution_clock::time_point(); + } +};