Skip to content

Commit

Permalink
Fix google#382 - MinGW often reports negative CPU times. (google#475)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EricWF authored and dominichamon committed Nov 7, 2017
1 parent f65c6d9 commit 72a4581
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ class ThreadTimer {
CHECK(running_);
running_ = false;
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
Expand Down

0 comments on commit 72a4581

Please sign in to comment.