为什么使用 std::execution::par 的排序比常规排序花费更多时间?

huangapple go评论52阅读模式
英文:

Why is sort with std::execution::par is taking longer than regular sort?

问题

为什么相同的向量v1(通常情况下)和v2(使用std::execution:parallel)对于v1花费的时间较短,而对于v2花费的时间较长?

我期望并行排序完成得更快。

结果:

626600 = 串行计数
1048100 = 并行计数
英文:

Why does the identical vectors v1 (normally) and v2 (with std::execution:parallel) takes less time for v1 and more time for v2?

I was expecting the parallel sort to complete faster.

#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm>
#include <execution>

using namespace std;

int main() {

    // Generate identical vectors v1 and v2 of length N
    // and fill them with v[i] = i/200;
    int N = 10000;
    vector<int> v1(N);
    for(int i=0; i<N; i++) v1[i] = i/200;
    auto v2 = v1;

    // Calculate the time taken to sort in serial way.
    auto start = std::chrono::system_clock::now();
    sort(v1.begin(), v1.end());
    auto end = std::chrono::system_clock::now();
    auto diff = end - start;
    cout << diff.count() << " = serial count" << endl;

    // Calculate the time taken to sort in parallel way using
    // std::execution::par
    start = std::chrono::system_clock::now();
    sort(std::execution::par, v2.begin(), v2.end());
    end = std::chrono::system_clock::now();
    diff = end - start;
    cout << diff.count() << " = parallel parallel" << endl;

    return 0;
}

Result:

626600 = serial count
1048100 = parallel count

答案1

得分: 3

int N = 10000; 是一个对于使用 std::execution::par 来说元素数量太小的情况。对于这么少的元素,运行和等待线程完成的开销可能会超过串行运行的时间。无论如何,我在 onlinegdb.com 上得到了不同的结果(使用 -O2 编译选项):

134692 = 串行计数
128443 = 并行计数
英文:

int N = 10000; is too small amount of elements for involving std::execution::par. Overheads for running and waiting for finishing threads can exceed serial run time with such a small amount of elements. In any case I get other results on onlinegdb.com (with -O2):

134692 = serial count
128443 = parallel count

huangapple
  • 本文由 发表于 2023年2月10日 11:31:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定