mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added a function to return a string of the time at recomended resolution
This commit is contained in:
@@ -26,6 +26,8 @@
|
||||
|
||||
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace mee{
|
||||
class Stopwatch{
|
||||
@@ -34,7 +36,7 @@ private:
|
||||
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
|
||||
enum TIME_RESOLUTION {HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND, DEFAULT};
|
||||
enum TIME_RESOLUTION {NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DEFAULT};
|
||||
//Return the duration in the default time period for the high_resolution_clock
|
||||
double getTime(TIME_RESOLUTION timeResolution){
|
||||
double timePassed = 0; //Holds the amount of time that has passed
|
||||
@@ -132,6 +134,37 @@ public:
|
||||
double getTime(){
|
||||
return getTime(DEFAULT);
|
||||
}
|
||||
//Returns a string with the time at best resolution
|
||||
std::string getStr(){
|
||||
//Setup the variables
|
||||
double tempTime = getTime(NANOSECOND); //Holds the
|
||||
std::stringstream timeStr;
|
||||
|
||||
//Decide what time resolution would be best. Looking for the format of XXX.XXX
|
||||
int timeRes = NANOSECOND;
|
||||
for(timeRes = MICROSECOND;(timeRes < DEFAULT) && (tempTime >= 1000);++timeRes){
|
||||
tempTime = getTime(static_cast<TIME_RESOLUTION>(timeRes));
|
||||
}
|
||||
--timeRes; //Take this variable back down to the right place. It has to go one too far to trigger the loop stop
|
||||
|
||||
//Put the number in the string
|
||||
timeStr << std::fixed << std::setprecision(3) << tempTime << ' ';
|
||||
|
||||
//From the timeRes variable decide what word should go on the end of the string
|
||||
switch(timeRes){
|
||||
case HOUR: timeStr << "hours"; break;
|
||||
case MINUTE: timeStr << "minutes"; break;
|
||||
case SECOND: timeStr << "seconds"; break;
|
||||
case MILLISECOND: timeStr << "milliseconds"; break;
|
||||
case MICROSECOND: timeStr << "microseconds"; break;
|
||||
case NANOSECOND: timeStr << "nanoseconds"; break;
|
||||
case DEFAULT: break;
|
||||
default: timeStr << "ERROR";
|
||||
}
|
||||
|
||||
//Return the string
|
||||
return timeStr.str();
|
||||
}
|
||||
//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
|
||||
|
||||
Reference in New Issue
Block a user