5 #ifndef MACHINE_LEARNING_TIMER_HPP 6 #define MACHINE_LEARNING_TIMER_HPP 19 using myClock = chrono::high_resolution_clock;
31 if (x == INT_MIN)
return 10 + 1;
32 if (x < 0)
return numDigits(-x) + 1;
68 static char x[256] = {0};
70 for (
char c = 1; c != 0; c++)
71 x[c] = numDigits((int32_t) c);
83 static string zeroPad(
int value,
unsigned int desiredSize) {
84 return string(desiredSize - numDigits(value),
'0').append(to_string(value));
93 explicit Timer(
unsigned int interval = 0,
unsigned int predictedIters = 0) :
95 predictedIters(predictedIters), lastUpdate(0) {
102 this->startTime = myClock::now();
111 chrono::time_point<chrono::system_clock> currentTime = myClock::now();
112 float totalSeconds = ((chrono::duration<float>) (currentTime - startTime)).count();
114 if (totalSeconds - lastUpdate > interval) {
115 lastUpdate = totalSeconds;
117 if (currentIter > 0 and predictedIters > 0) {
118 float estimatedTotalSeconds = (totalSeconds / (currentIter)) * predictedIters;
119 string formattedTotalTime =
Timer::prettyTime(estimatedTotalSeconds - totalSeconds);
121 cout <<
"it " << currentIter + 1 <<
"/" << predictedIters <<
" (est. " << formattedTotalTime <<
")" << endl;
135 int totalSeconds = (int) secondsFloat;
136 int milliseconds = (int) (secondsFloat * 1000) % 1000;
137 int seconds = totalSeconds % 60;
138 int minutes = (totalSeconds / 60) % 60;
139 int hours = (totalSeconds / (60 * 60)) % 24;
141 string formattedTime = to_string(hours) +
":" + zeroPad(minutes, 2) +
":" 142 + zeroPad(seconds, 2) +
"." + zeroPad(milliseconds, 3);
143 return formattedTime;
150 float totalSeconds = ((chrono::duration<float>) (myClock::now() - startTime)).count();
155 #endif //MACHINE_LEARNING_TIMER_HPP
Timer(unsigned int interval=0, unsigned int predictedIters=0)
Creates an instance of a timer object.
static int numDigits(char n)
Counts the number of digits in a char.
k-nearest neighbors algorithm, able to do regression and classification
static string prettyTime(float secondsFloat)
Formats seconds as HH:MM:SS.mmm.
static string zeroPad(int value, unsigned int desiredSize)
Pads a number with 0s on the left until it reaches a desired length.
bool activate(unsigned int currentIter=0)
Checks if the time interval passed in the constructor has passed.
void start()
Start the timer.
unsigned int predictedIters
static int numDigits(int32_t x)
Counts the number of digits in an integer.
chrono::time_point< chrono::system_clock > startTime
A timer that keeps track of time, prints formatted time, checks if an interval has passed etc...
chrono::high_resolution_clock myClock