Repository: google/benchmark
File: src/counter.cc (C++)

Code before:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "counter.h"

namespace benchmark {
namespace internal {

namespace {

double Finish(Counter const& c, IterationCount iterations, double cpu_time,
              double num_threads) {
  double v = c.value;
  if ((c.flags & Counter::kIsRate) != 0) {
    // The CPU time is the total time taken by all threads (https://github.com/google/benchmark/pull/1836)
    // If we used that as the denominator, we'd be calculating the rate per thread here. This is why
    // we have to divide the total cpu_time by the number of threads for global counters to get a global rate.
    //
    // See https://github.com/google/benchmark/issues/2080
    v /= (cpu_time / num_threads);
  }
  if ((c.flags & Counter::kAvgThreads) != 0) {
    v /= num_threads;
  }
  if ((c.flags & Counter::kIsIterationInvariant) != 0) {
    v *= static_cast<double>(iterations);
  }
  if ((c.flags & Counter::kAvgIterations) != 0) {
    v /= static_cast<double>(iterations);
  }

  if ((c.flags & Counter::kInvert) != 0) {  // Invert is *always* last.
    v = 1.0 / v;
  }
  return v;
}

}  // namespace

void Finish(UserCounters* l, IterationCount iterations, double cpu_time,
            double num_threads) {
  for (auto& c : *l) {
    c.second.value = Finish(c.second, iterations, cpu_time, num_threads);
  }
}

Review comment (suggestion):
Please hoist the `cpu_time / num_threads` into the outermost caller of `Finish()`.

Code after:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "counter.h"

namespace benchmark {
namespace internal {

namespace {

double Finish(Counter const& c, IterationCount iterations, double cpu_time,
              double num_threads) {
  double v = c.value;
  if ((c.flags & Counter::kIsRate) != 0) {
    v /= cpu_time;
  }
  if ((c.flags & Counter::kAvgThreads) != 0) {
    v /= num_threads;
  }
  if ((c.flags & Counter::kIsIterationInvariant) != 0) {
    v *= static_cast<double>(iterations);
  }
  if ((c.flags & Counter::kAvgIterations) != 0) {
    v /= static_cast<double>(iterations);
  }

  if ((c.flags & Counter::kInvert) != 0) {  // Invert is *always* last.
    v = 1.0 / v;
  }
  return v;
}

}  // namespace

void Finish(UserCounters* l, IterationCount iterations, double cpu_time,
            double num_threads) {
  for (auto& c : *l) {
    c.second.value = Finish(c.second, iterations, cpu_time, num_threads);
  }
}

void Increment(UserCounters* l, UserCounters const& r) {
  // add counters present in both or just in *l
  for (auto& c : *l) {
    auto it = r.find(c.first);