Use the variable name _ for unused return values
[bitcoinplatinum.git] / src / bench / bench.h
blob1f36f2a4bca1b07e2577aebb8392463aabe0a2b4
1 // Copyright (c) 2015-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_BENCH_BENCH_H
6 #define BITCOIN_BENCH_BENCH_H
8 #include <functional>
9 #include <limits>
10 #include <map>
11 #include <string>
13 #include <boost/preprocessor/cat.hpp>
14 #include <boost/preprocessor/stringize.hpp>
16 // Simple micro-benchmarking framework; API mostly matches a subset of the Google Benchmark
17 // framework (see https://github.com/google/benchmark)
18 // Why not use the Google Benchmark framework? Because adding Yet Another Dependency
19 // (that uses cmake as its build system and has lots of features we don't need) isn't
20 // worth it.
23 * Usage:
25 static void CODE_TO_TIME(benchmark::State& state)
27 ... do any setup needed...
28 while (state.KeepRunning()) {
29 ... do stuff you want to time...
31 ... do any cleanup needed...
34 BENCHMARK(CODE_TO_TIME);
38 namespace benchmark {
40 class State {
41 std::string name;
42 double maxElapsed;
43 double beginTime;
44 double lastTime, minTime, maxTime, countMaskInv;
45 uint64_t count;
46 uint64_t countMask;
47 uint64_t beginCycles;
48 uint64_t lastCycles;
49 uint64_t minCycles;
50 uint64_t maxCycles;
51 public:
52 State(std::string _name, double _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) {
53 minTime = std::numeric_limits<double>::max();
54 maxTime = std::numeric_limits<double>::min();
55 minCycles = std::numeric_limits<uint64_t>::max();
56 maxCycles = std::numeric_limits<uint64_t>::min();
57 countMask = 1;
58 countMaskInv = 1./(countMask + 1);
60 bool KeepRunning();
63 typedef std::function<void(State&)> BenchFunction;
65 class BenchRunner
67 typedef std::map<std::string, BenchFunction> BenchmarkMap;
68 static BenchmarkMap &benchmarks();
70 public:
71 BenchRunner(std::string name, BenchFunction func);
73 static void RunAll(double elapsedTimeForOne=1.0);
77 // BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo", foo);
78 #define BENCHMARK(n) \
79 benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n);
81 #endif // BITCOIN_BENCH_BENCH_H