Skip to content

Commit 7072c54

Browse files
committed
Support very-fast-running benchmarks
Avoid calling gettimeofday every time through the benchmarking loop, by keeping track of how long each loop takes and doubling the number of iterations done between time checks when they take less than 1/16'th of the total elapsed time.
1 parent 535ed92 commit 7072c54

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ bench_bench_bitcoin_SOURCES = \
77
bench/bench_bitcoin.cpp \
88
bench/bench.cpp \
99
bench/bench.h \
10-
bench/MilliSleep.cpp
10+
bench/Examples.cpp
1111

1212
bench_bench_bitcoin_CPPFLAGS = $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/
1313
bench_bench_bitcoin_LDADD = \

src/bench/MilliSleep.cpp renamed to src/bench/Examples.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "main.h"
77
#include "utiltime.h"
88

9+
// Sanity test: this should loop ten times, and
10+
// min/max/average should be close to 100ms.
911
static void Sleep100ms(benchmark::State& state)
1012
{
1113
while (state.KeepRunning()) {
@@ -14,3 +16,19 @@ static void Sleep100ms(benchmark::State& state)
1416
}
1517

1618
BENCHMARK(Sleep100ms);
19+
20+
// Extremely fast-running benchmark:
21+
#include <math.h>
22+
23+
volatile double sum = 0.0; // volatile, global so not optimized away
24+
25+
static void Trig(benchmark::State& state)
26+
{
27+
double d = 0.01;
28+
while (state.KeepRunning()) {
29+
sum += sin(d);
30+
d += 0.000001;
31+
}
32+
}
33+
34+
BENCHMARK(Trig);

src/bench/bench.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ BenchRunner::RunAll(double elapsedTimeForOne)
3636

3737
bool State::KeepRunning()
3838
{
39-
double now = gettimedouble();
39+
double now;
4040
if (count == 0) {
41-
beginTime = now;
41+
beginTime = now = gettimedouble();
4242
}
4343
else {
44-
double elapsedOne = now - lastTime;
44+
// timeCheckCount is used to avoid calling gettime most of the time,
45+
// so benchmarks that run very quickly get consistent results.
46+
if ((count+1)%timeCheckCount != 0) {
47+
++count;
48+
return true; // keep going
49+
}
50+
now = gettimedouble();
51+
double elapsedOne = (now - lastTime)/timeCheckCount;
4552
if (elapsedOne < minTime) minTime = elapsedOne;
4653
if (elapsedOne > maxTime) maxTime = elapsedOne;
54+
if (elapsedOne*timeCheckCount < maxElapsed/16) timeCheckCount *= 2;
4755
}
4856
lastTime = now;
4957
++count;

src/bench/bench.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ namespace benchmark {
4141
double beginTime;
4242
double lastTime, minTime, maxTime;
4343
int64_t count;
44+
int64_t timeCheckCount;
4445
public:
4546
State(std::string _name, double _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) {
4647
minTime = std::numeric_limits<double>::max();
4748
maxTime = std::numeric_limits<double>::min();
49+
timeCheckCount = 1;
4850
}
4951
bool KeepRunning();
5052
};

0 commit comments

Comments
 (0)