Commit 72a4581c by Eric Committed by Dominic Hamon

Fix #382 - MinGW often reports negative CPU times. (#475)

When stopping a timer, the current time is subtracted from the start time. However, when the times are identical, or sufficiently close together, the subtraction can result in a negative number. For some reason MinGW is the only platform where this problem manifests. I suspect it's due to MinGW specific behavior in either the CPU timing code, floating point model, or printf formatting. Either way, the fix for MinGW should be correct across all platforms.
parent f65c6d9a
...@@ -175,7 +175,9 @@ class ThreadTimer { ...@@ -175,7 +175,9 @@ class ThreadTimer {
CHECK(running_); CHECK(running_);
running_ = false; running_ = false;
real_time_used_ += ChronoClockNow() - start_real_time_; real_time_used_ += ChronoClockNow() - start_real_time_;
cpu_time_used_ += ThreadCPUUsage() - start_cpu_time_; // Floating point error can result in the subtraction producing a negative
// time. Guard against that.
cpu_time_used_ += std::max<double>(ThreadCPUUsage() - start_cpu_time_, 0);
} }
// Called by each thread // Called by each thread
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment