我正在尝试比较使用 Google Benchstd::valarray
与std::vector
/操作的性能。std::transform
我正在使用 QuickBench。
我的代码(用于 QuickBench)是
#include <random>
class RG {
public:
double operator()() noexcept { return dis(gen); }
private:
std::mt19937 gen {std::random_device{}()};
std::uniform_real_distribution<> dis {0.0, 100.0};
};
static RG rg {};
static constexpr auto N = 1000;
#include <vector>
#include <algorithm>
static void Vector(benchmark::State& state) {
std::vector<double> v1, v2;
v1.reserve(N); v2.reserve(N);
std::generate_n(back_inserter(v1),N,rg);
std::generate_n(back_inserter(v2),N,rg);
for (auto _ : state) {
std::vector<double> v3; v3.reserve(N);
std::transform(cbegin(v1), cend(v1), cbegin(v2), back_inserter(v3),
[](auto d1, auto d2) noexcept { return 0.5*d1+1.5*d2;});
benchmark::DoNotOptimize(v3);
}
}
// Register the function as a benchmark
BENCHMARK(Vector);
#include <valarray>
static void ValArray(benchmark::State& state) {
std::valarray<double> v1{N}, v2{N};
std::generate(begin(v1),end(v1),rg);
std::generate(begin(v2),end(v2),rg);
for (auto _ : state) {
std::valarray<double> v3{};
v3=0.5*v1+1.5*v2;
benchmark::DoNotOptimize(v3);
}
}
BENCHMARK(ValArray);
QuickBench 链接
Quickbench 说std::valarray
是 72 倍那么快std::vector
。那不可能是对的,对吧?我究竟做错了什么?