Skip to content

Commit

Permalink
Merge pull request #2302 from Snektron/timer-to-micros-chrono
Browse files Browse the repository at this point in the history
metrics: use chrono to convert ticks to micros
  • Loading branch information
jhasse authored Nov 29, 2023
2 parents 82a7cb3 + dd8f62c commit 09b6b4e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ constexpr int64_t GetFrequency() {

int64_t TimerToMicros(int64_t dt) {
// dt is in ticks. We want microseconds.
return (dt * 1000000) / GetFrequency();
return chrono::duration_cast<chrono::microseconds>(
std::chrono::steady_clock::duration{ dt })
.count();
}

int64_t TimerToMicros(double dt) {
// dt is in ticks. We want microseconds.
return (dt * 1000000) / GetFrequency();
using DoubleSteadyClock =
std::chrono::duration<double, std::chrono::steady_clock::period>;
return chrono::duration_cast<chrono::microseconds>(DoubleSteadyClock{ dt })
.count();
}

} // anonymous namespace
Expand Down

0 comments on commit 09b6b4e

Please sign in to comment.