Skip to content

Commit a24633a

Browse files
committed
Remove countMaskInv caching in bench framework
- backports bitcoin/bitcoin@0b1b914 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.
1 parent 9e9bc22 commit a24633a

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

src/bench/bench.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,20 @@ bool benchmark::State::KeepRunning()
5656
else {
5757
now = gettimedouble();
5858
double elapsed = now - lastTime;
59-
double elapsedOne = elapsed * countMaskInv;
59+
double elapsedOne = elapsed / (countMask + 1);
6060
if (elapsedOne < minTime) minTime = elapsedOne;
6161
if (elapsedOne > maxTime) maxTime = elapsedOne;
6262

6363
// We only use relative values, so don't have to handle 64-bit wrap-around specially
6464
nowCycles = perf_cpucycles();
65-
uint64_t elapsedOneCycles = (nowCycles - lastCycles) * countMaskInv;
65+
uint64_t elapsedOneCycles = (nowCycles - lastCycles) / (countMask + 1);
6666
if (elapsedOneCycles < minCycles) minCycles = elapsedOneCycles;
6767
if (elapsedOneCycles > maxCycles) maxCycles = elapsedOneCycles;
6868

6969
if (elapsed*128 < maxElapsed) {
7070
// If the execution was much too fast (1/128th of maxElapsed), increase the count mask by 8x and restart timing.
7171
// The restart avoids including the overhead of this code in the measurement.
7272
countMask = ((countMask<<3)|7) & ((1LL<<60)-1);
73-
countMaskInv = 1./(countMask+1);
7473
count = 0;
7574
minTime = std::numeric_limits<double>::max();
7675
maxTime = std::numeric_limits<double>::min();
@@ -82,7 +81,6 @@ bool benchmark::State::KeepRunning()
8281
uint64_t newCountMask = ((countMask<<1)|1) & ((1LL<<60)-1);
8382
if ((count & newCountMask)==0) {
8483
countMask = newCountMask;
85-
countMaskInv = 1./(countMask+1);
8684
}
8785
}
8886
}

src/bench/bench.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace benchmark {
3939
std::string name;
4040
double maxElapsed;
4141
double beginTime;
42-
double lastTime, minTime, maxTime, countMaskInv;
42+
double lastTime, minTime, maxTime;
4343
uint64_t count;
4444
uint64_t countMask;
4545
uint64_t beginCycles;
@@ -53,7 +53,6 @@ namespace benchmark {
5353
minCycles = std::numeric_limits<uint64_t>::max();
5454
maxCycles = std::numeric_limits<uint64_t>::min();
5555
countMask = 1;
56-
countMaskInv = 1./(countMask + 1);
5756
}
5857
bool KeepRunning();
5958
};

0 commit comments

Comments
 (0)