我是谷歌基准工具的新手,我正在测试所有的操作时间。请在下面找到我的代码。在这段代码中,我正在测试每个操作(添加/子/mul/div)将在我的硬件上花费多少时间。
#include <benchmark/benchmark.h>
double min(double)
{
return 2.2;
}
double max(double)
{
return 8.8;
}
int min(int)
{
return 2;
}
int max(int)
{
return 8;
}
template <typename Data>
void bench_mark_mul(benchmark::State &state)
{
Data a, b;
a = min(a);
b = max(b);
while (state.KeepRunning())
{
benchmark::DoNotOptimize(a * b);
}
}
template <typename Data>
void bench_mark_div(benchmark::State &state)
{
Data a, b;
a = min(a);
b = max(b);
while (state.KeepRunning())
{
benchmark::DoNotOptimize(a / b);
}
}
template <typename Data>
void bench_mark_sum(benchmark::State &state)
{
Data a, b;
a = min(a);
b = max(b);
while (state.KeepRunning())
{
benchmark::DoNotOptimize(a + b);
}
}
template <typename Data>
void bench_mark_dif(benchmark::State &state)
{
Data a, b;
a = min(a);
b = max(b);
while (state.KeepRunning())
{
benchmark::DoNotOptimize(a - b);
}
}
BENCHMARK_TEMPLATE1(bench_mark_dif, double);
BENCHMARK_TEMPLATE1(bench_mark_sum, double);
BENCHMARK_TEMPLATE1(bench_mark_mul, double);
BENCHMARK_TEMPLATE1(bench_mark_div, double);
BENCHMARK_TEMPLATE1(bench_mark_dif, int);
BENCHMARK_TEMPLATE1(bench_mark_sum, int);
BENCHMARK_TEMPLATE1(bench_mark_mul, int);
BENCHMARK_TEMPLATE1(bench_mark_div, int);
BENCHMARK_MAIN();
我得到了低于结果。
为什么双除法时间少于 int 除法?正如我得出的结论,这是由于我们在除数时从浮点数转换为整数?