名稱空間
變體
操作

std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
execution::seqexecution::parexecution::par_unseqexecution::unseq
(C++17)    (C++17)(C++17)(C++20)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <execution>
inline constexpr
std::execution::sequenced_policy seq { /* unspecified */ };
(C++17 起)
inline constexpr
std::execution::parallel_policy par { /* unspecified */ };
(C++17 起)
inline constexpr
std::execution::parallel_unsequenced_policy par_unseq { /* unspecified */ };
(C++17 起)
inline constexpr
std::execution::unsequenced_policy unseq { /* unspecified */ };
(C++20 起)

執行策略型別

具有以下相應的例項

  • std::execution::seq,
  • std::execution::par,
  • std::execution::par_unseq, 以及
  • std::execution::unseq.

這些例項用於指定並行演算法的執行策略,即允許的並行型別。

標準庫實現可以提供額外的執行策略(未來可能新增的包括 std::parallel::cudastd::parallel::opencl)。

[編輯] 示例

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <random>
#include <vector>
 
#ifdef PARALLEL
#include <execution>
    namespace execution = std::execution;
#else
    enum class execution { seq, unseq, par_unseq, par };
#endif
 
void measure([[maybe_unused]] auto policy, std::vector<std::uint64_t> v)
{
    const auto start = std::chrono::steady_clock::now();
#ifdef PARALLEL
    std::sort(policy, v.begin(), v.end());
#else
    std::sort(v.begin(), v.end());
#endif
    const auto finish = std::chrono::steady_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start)
              << '\n';
};
 
int main()
{
    std::vector<std::uint64_t> v(1'000'000);
    std::mt19937 gen {std::random_device{}()};
    std::ranges::generate(v, gen);
 
    measure(execution::seq, v);
    measure(execution::unseq, v);
    measure(execution::par_unseq, v);
    measure(execution::par, v);
}

可能的輸出

// online GNU/gcc compiler (PARALLEL macro is not defined)
81ms
80ms
79ms
78ms
 
// with g++ -std=c++23 -O3 ./test.cpp -ltbb -DPARALLEL
165ms
163ms
30ms
27ms

[編輯] 參閱

執行策略型別
(類) [編輯]