mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
102 lines
2.3 KiB
C++
102 lines
2.3 KiB
C++
//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
|
|
/*
|
|
Copyright (C) 2018 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;
|
|
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<double> dur = endTime - startTime;
|
|
return dur.count();
|
|
}
|
|
else{
|
|
return 0;
|
|
}
|
|
}
|
|
double getMilli(){
|
|
if(hasStarted && hasEnded){
|
|
std::chrono::duration<double, std::milli> dur = endTime - startTime;
|
|
return dur.count();
|
|
}
|
|
else{
|
|
return 0;
|
|
}
|
|
}
|
|
double getMicro(){
|
|
if(hasStarted && hasEnded){
|
|
std::chrono::duration<double, std::micro> dur = endTime - startTime;
|
|
return dur.count();
|
|
}
|
|
else{
|
|
return 0;
|
|
}
|
|
}
|
|
double getNano(){
|
|
if(hasStarted && hasEnded){
|
|
std::chrono::duration<double, std::nano> dur = endTime - startTime;
|
|
return dur.count();
|
|
}
|
|
else{
|
|
return 0;
|
|
}
|
|
}
|
|
void reset(){
|
|
hasStarted = hasEnded = false;
|
|
endTime = startTime = std::chrono::high_resolution_clock::time_point();
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif //end STOPWATCH_HPP
|