mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
86 lines
1.6 KiB
C++
86 lines
1.6 KiB
C++
//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
|
|
|
|
#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
|