gpt4 book ai didi

c++ - 谷歌/基准测试结果不一致

转载 作者:行者123 更新时间:2023-11-28 04:33:54 32 4
gpt4 key购买 nike

我是使用 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 结果:

最佳答案

基准测试结果取决于平台/架构/机器。当您在同一台机器上运行基准测试时,假设它们始终相同是不切实际的,温度、性能扩展选项、磨损等都会影响性能。

关于c++ - 谷歌/基准测试结果不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52076296/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com