我是使用 Google Benchmark 的新手,并且在运行相同的基准(如下)时收到不同的结果,当在本地运行代码与在Quick-Bench.com上运行代码时,它使用 C++ 检索本地时间。两次我都使用了 GCC 8.2 和 -O3。
为什么在本地运行与在 quick-bench.com 上运行的结果差异很大?哪个是对的?
#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);
在本地产生以下结果:
-------------------------------------------------------------
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 结果: