15 static std::map<std::string, benchmark::BenchFunction> benchmarks_map;
16 return benchmarks_map;
19 static double gettimedouble(
void) {
21 gettimeofday(&tv,
nullptr);
22 return tv.tv_usec * 0.000001 + tv.tv_sec;
27 benchmarks().insert(std::make_pair(name, func));
34 std::cout <<
"#Benchmark" <<
"," <<
"count" <<
"," <<
"min" <<
"," <<
"max" <<
"," <<
"average" <<
"," 35 <<
"min_cycles" <<
"," <<
"max_cycles" <<
"," <<
"average_cycles" <<
"\n";
38 State state(p.first, elapsedTimeForOne);
46 if (count & countMask) {
53 lastTime = beginTime = now = gettimedouble();
57 now = gettimedouble();
58 double elapsed = now - lastTime;
59 double elapsedOne = elapsed / (countMask + 1);
60 if (elapsedOne < minTime) minTime = elapsedOne;
61 if (elapsedOne > maxTime) maxTime = elapsedOne;
65 uint64_t elapsedOneCycles = (nowCycles - lastCycles) / (countMask + 1);
66 if (elapsedOneCycles < minCycles) minCycles = elapsedOneCycles;
67 if (elapsedOneCycles > maxCycles) maxCycles = elapsedOneCycles;
69 if (elapsed*128 < maxElapsed) {
72 countMask = ((countMask<<3)|7) & ((1LL<<60)-1);
74 minTime = std::numeric_limits<double>::max();
75 maxTime = std::numeric_limits<double>::min();
76 minCycles = std::numeric_limits<uint64_t>::max();
77 maxCycles = std::numeric_limits<uint64_t>::min();
80 if (elapsed*16 < maxElapsed) {
81 uint64_t newCountMask = ((countMask<<1)|1) & ((1LL<<60)-1);
82 if ((count & newCountMask)==0) {
83 countMask = newCountMask;
88 lastCycles = nowCycles;
91 if (now - beginTime < maxElapsed)
return true;
95 assert(count != 0 &&
"count == 0 => (now == 0 && beginTime == 0) => return above");
98 double average = (now-beginTime)/count;
99 int64_t averageCycles = (nowCycles-beginCycles)/count;
100 std::cout << std::fixed << std::setprecision(15) << name <<
"," << count <<
"," << minTime <<
"," << maxTime <<
"," << average <<
"," 101 << minCycles <<
"," << maxCycles <<
"," << averageCycles <<
"\n";
102 std::cout.copyfmt(std::ios(
nullptr));
BenchRunner(std::string name, BenchFunction func)
static void RunAll(double elapsedTimeForOne=1.0)
uint64_t perf_cpucycles(void)
Functions for measurement of CPU cycles.
std::map< std::string, BenchFunction > BenchmarkMap
std::function< void(State &)> BenchFunction
static BenchmarkMap & benchmarks()