feat: implement ostream operator for timer

This commit is contained in:
Michael Mandl 2024-03-20 10:13:08 +01:00
parent 64ae4c62a9
commit 42b8c8d7e7
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7
3 changed files with 23 additions and 3 deletions

View file

@ -12,3 +12,21 @@ long Timer::us() const {
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_)
.count();
}
std::ostream &operator<<(std::ostream &os, const Timer &timer) {
long time = timer.us();
if (time >= 1e6) {
os << time / static_cast<long>(1e6) << " s ";
time %= static_cast<long>(1e6);
}
if (time >= 1e3) {
os << time / static_cast<long>(1e3) << " ms ";
time %= static_cast<long>(1e3);
}
os << time << " µs";
return os;
}