C++ 为什么并行执行策略如此缓慢

C++ 为什么并行执行策略如此缓慢,c++,c++17,C++,C++17,我尝试使用以下两个版本的函数: template <typename T, typename U> constexpr auto mult(T const& a, U const& b) { return std::transform_reduce(a.begin(), a.end(), b.begin(), 0.); //return std::transform_reduce(std::execution::par, a.begin(), a.end(),

我尝试使用以下两个版本的函数:

template <typename T, typename U>
constexpr auto mult(T const& a, U const& b) {
  return std::transform_reduce(a.begin(), a.end(), b.begin(), 0.);
  //return std::transform_reduce(std::execution::par, a.begin(), a.end(), b.begin(), 0.);
}
模板
constexpr自动多路传输(T const&a、U const&b){
返回std::transform_reduce(a.begin(),a.end(),b.begin(),0);
//返回std::transform_reduce(std::execution::par,a.begin(),a.end(),b.begin(),0);
}

我使用2
std::array
s调用此函数。第二个版本(上面注释掉)的运行速度比第一个版本慢约40倍。我使用的是VS2019(16.3.1),在发布时编译。

@MaximeGroushkin您是对的。第三个迭代器也仅用于读取,因此它不应该是一个问题。@NathanOliver的范围从几十个到~1000个(最大值),但在数千个数据集上被调用。您的数据集非常小。旋转线程需要花费时间和资源,可能只是额外的开销远远超过了并行化带来的任何好处。@NathanOliver我尝试了10000个数据集,结果类似(只慢了10倍左右),这应该足够大,足以产生不同。对于更大的输入,非并行输入总是更快。[我还测试了gcc(),它对大型数据集给出了类似的结果。我不确定这里发生了什么。