Files
Upsilon/quiz/src/stopwatch.cpp
Ruben Dashyan bc0b21463e [quiz][ion/test] Move time measurements to quiz
to be able to do measurements in any test
2020-06-04 14:50:00 +02:00

37 lines
962 B
C++

#include <quiz/stopwatch.h>
#include "quiz.h"
#include <ion/timing.h>
#include <string.h>
uint64_t quiz_stopwatch_start() {
return Ion::Timing::millis();
}
static size_t uint64ToString(uint64_t n, char buffer[]) {
size_t len = 0;
do {
buffer[len++] = (n % 10) + '0';
} while ((n /= 10) > 0);
int i = 0;
int j = len - 1;
while (i < j) {
char c = buffer[i];
buffer[i++] = buffer[j];
buffer[j--] = c;
}
return len;
}
void quiz_stopwatch_print_lap(uint64_t startTime) {
constexpr char Time[] = " time: ";
constexpr char Ms[] = "ms";
constexpr size_t uint64ToStringMaxLength = 20;
constexpr size_t MaxLength = sizeof(Time) + uint64ToStringMaxLength + sizeof(Ms) + 1;
char buffer[MaxLength];
char * position = buffer;
position += strlcpy(position, Time, sizeof(Time));
position += uint64ToString(Ion::Timing::millis() - startTime, position);
position += strlcpy(position, Ms, sizeof(Ms));
quiz_print(buffer);
}