I'm new to using Google Benchmark and receive different results running the same benchmark (below), which retrieves the local time using C++, when running the code locally vs on Quick-Bench.com. Both times I used GCC 8.2 and -O3.
Why do the results vary dramatically between running locally vs on quick-bench.com? Which is correct?
#include <benchmark/benchmark.h>
#include <ctime>
#include <sys/time.h>
#include <chrono>
static void BM_ctime(benchmark::State& state) {
unsigned long long count = 0;
for (auto _ : state) {
std::time_t sec = std::time(0);
benchmark::DoNotOptimize(count += sec);
}
}
BENCHMARK(BM_ctime);
static void BM_sysTime(benchmark::State& state) {
unsigned long long count = 0;
for (auto _ : state) {
unsigned long sec = time(NULL);
benchmark::DoNotOptimize(count += sec);
}
}
BENCHMARK(BM_sysTime);
static void BM_chronoMilliseconds(benchmark::State& state) {
unsigned long long count = 0;
for (auto _ : state) {
unsigned long long ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
benchmark::DoNotOptimize(count += ms);
}
}
BENCHMARK(BM_chronoMilliseconds);
static void BM_chronoSececonds(benchmark::State& state) {
unsigned long long count = 0;
for (auto _ : state) {
unsigned long long sec = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
benchmark::DoNotOptimize(count += sec);
}
}
BENCHMARK(BM_chronoSececonds);
Locally the following results are produced:
-------------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------------
BM_ctime 183 ns 175 ns 4082013
BM_sysTime 197 ns 179 ns 4004829
BM_chronoMilliseconds 37 ns 36 ns 19092506
BM_chronoSececonds 37 ns 36 ns 19057991
QuickBench results: