Browse Source

Util: Add fancyprint function

master
Riyyi 3 years ago
parent
commit
2b2bd7ff78
  1. 20
      src/util/timer.cpp
  2. 2
      src/util/timer.h

20
src/util/timer.cpp

@ -1,6 +1,6 @@
#include <algorithm> // max, min #include <chrono> // high_resolution_clock, seconds, milliseconds, microseconds, nanoseconds
#include <chrono> // high_resolution_clock seconds, milliseconds, microseconds, nanoseconds
#include <cstdint> // uint64_t #include <cstdint> // uint64_t
#include <cstdio> // printf
#include "util/timer.h" #include "util/timer.h"
@ -69,4 +69,20 @@ uint64_t Timer::elapsedNanoseconds()
return elapsed<std::chrono::nanoseconds>(); return elapsed<std::chrono::nanoseconds>();
} }
void Timer::fancyPrint(uint64_t nanoseconds)
{
if (nanoseconds > 999999999) {
printf("%.3fs", nanoseconds / 1000000000.0);
}
else if (nanoseconds > 999999) {
printf("%.0fms", nanoseconds / 1000000.0);
}
else if (nanoseconds > 999) {
printf("%.0fμs", nanoseconds / 1000.0);
}
else {
printf("%luns", nanoseconds);
}
}
} // namespace Util } // namespace Util

2
src/util/timer.h

@ -32,6 +32,8 @@ public:
uint64_t elapsedMicroseconds(); uint64_t elapsedMicroseconds();
uint64_t elapsedNanoseconds(); uint64_t elapsedNanoseconds();
static void fancyPrint(uint64_t nanoseconds);
const TimePoint& start() const { return m_start; } const TimePoint& start() const { return m_start; }
private: private:

Loading…
Cancel
Save