From a24633a8a91c808853310b0055e8f1d86bd008e9 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Wed, 27 May 2020 22:09:04 +0200 Subject: [PATCH] Remove countMaskInv caching in bench framework - backports bitcoin@0b1b9148cd77092d2851eeed5c8c6d5ce117452a We were saving a div by caching the inverse as a float, but this ended up requiring a int -> float -> int conversion, which takes almost as much time as the difference between float mul and div. There are lots of other more pressing issues with the bench framework which probably require simply removing the adaptive iteration count stuff anyway. --- src/bench/bench.cpp | 6 ++---- src/bench/bench.h | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index 6de4802c146dd..5576a53c99892 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -56,13 +56,13 @@ bool benchmark::State::KeepRunning() else { now = gettimedouble(); double elapsed = now - lastTime; - double elapsedOne = elapsed * countMaskInv; + double elapsedOne = elapsed / (countMask + 1); if (elapsedOne < minTime) minTime = elapsedOne; if (elapsedOne > maxTime) maxTime = elapsedOne; // We only use relative values, so don't have to handle 64-bit wrap-around specially nowCycles = perf_cpucycles(); - uint64_t elapsedOneCycles = (nowCycles - lastCycles) * countMaskInv; + uint64_t elapsedOneCycles = (nowCycles - lastCycles) / (countMask + 1); if (elapsedOneCycles < minCycles) minCycles = elapsedOneCycles; if (elapsedOneCycles > maxCycles) maxCycles = elapsedOneCycles; @@ -70,7 +70,6 @@ bool benchmark::State::KeepRunning() // If the execution was much too fast (1/128th of maxElapsed), increase the count mask by 8x and restart timing. // The restart avoids including the overhead of this code in the measurement. countMask = ((countMask<<3)|7) & ((1LL<<60)-1); - countMaskInv = 1./(countMask+1); count = 0; minTime = std::numeric_limits::max(); maxTime = std::numeric_limits::min(); @@ -82,7 +81,6 @@ bool benchmark::State::KeepRunning() uint64_t newCountMask = ((countMask<<1)|1) & ((1LL<<60)-1); if ((count & newCountMask)==0) { countMask = newCountMask; - countMaskInv = 1./(countMask+1); } } } diff --git a/src/bench/bench.h b/src/bench/bench.h index 6b1fae9a638fa..4eb5b79018add 100644 --- a/src/bench/bench.h +++ b/src/bench/bench.h @@ -39,7 +39,7 @@ namespace benchmark { std::string name; double maxElapsed; double beginTime; - double lastTime, minTime, maxTime, countMaskInv; + double lastTime, minTime, maxTime; uint64_t count; uint64_t countMask; uint64_t beginCycles; @@ -53,7 +53,6 @@ namespace benchmark { minCycles = std::numeric_limits::max(); maxCycles = std::numeric_limits::min(); countMask = 1; - countMaskInv = 1./(countMask + 1); } bool KeepRunning(); };