mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added a stopwatch class that allows easy timing of programs
This commit is contained in:
80
Stopwatch.hpp
Normal file
80
Stopwatch.hpp
Normal file
@@ -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 <chrono>
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user