标准库标头 <execution> (C++17)学习

此头文件是算法库的一部分。本篇介绍策略类型的一些应用示例:

is_execution_policy

(C++17)

测试一个类是否表示某种执行策略
(类模板)

在命名空间 std::execution 定义

sequenced_policyparallel_policyparallel_unsequenced_policyunsequenced_policy

(C++17)(C++17)(C++17)(C++20)

执行策略类型
(类)

常量

在命名空间 std::execution 定义

seqparpar_unsequnseq

(C++17)(C++17)(C++17)(C++20)

全局执行策略对象
(常量)

 首先看execution在VS2022里的定义

namespace execution {_EXPORT_STD class sequenced_policy {// indicates support for only sequential execution, and requests termination on exceptionspublic:using _Standard_execution_policy   = int;static constexpr bool _Parallelize = false;static constexpr bool _Ivdep       = false;};_EXPORT_STD inline constexpr sequenced_policy seq{/* unspecified */};_EXPORT_STD class parallel_policy {// indicates support by element access functions for parallel execution with parallel forward progress// guarantees, and requests termination on exceptionspublic:using _Standard_execution_policy   = int;static constexpr bool _Parallelize = true;static constexpr bool _Ivdep       = true;};_EXPORT_STD inline constexpr parallel_policy par{/* unspecified */};_EXPORT_STD class parallel_unsequenced_policy {// indicates support by element access functions for parallel execution with weakly parallel forward progress// guarantees, and requests termination on exceptions//// (at this time, equivalent to parallel_policy)public:using _Standard_execution_policy   = int;static constexpr bool _Parallelize = true;static constexpr bool _Ivdep       = true;};_EXPORT_STD inline constexpr parallel_unsequenced_policy par_unseq{/* unspecified */};#if _HAS_CXX20_EXPORT_STD class unsequenced_policy {// indicates support by element access functions for weakly parallel forward progress guarantees, and for// executing interleaved on the same thread, and requests termination on exceptions//// (at this time, equivalent to sequenced_policy except for for_each(_n), destroy(_n),// uninitialized_default_construct(_n), and uninitialized_value_construct(_n))public:using _Standard_execution_policy   = int;static constexpr bool _Parallelize = false;static constexpr bool _Ivdep       = true;};_EXPORT_STD inline constexpr unsequenced_policy unseq{/* unspecified */};
#endif // _HAS_CXX20} // namespace execution

示例代码:

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include <execution>
#include <string>//https://zh.cppreference.com/w/cpp/algorithm/execution_policy_tag
//https://blog.csdn.net/CUBE_lotus/article/details/137159423#ifdef PARALLEL
#include <execution>
namespace execution = std::execution;
#else
enum class execution { seq, unseq, par_unseq, par };
#endifvoid measure([[maybe_unused]] auto policy, std::vector<std::uint64_t> v)
{const auto start = std::chrono::steady_clock::now();
#ifdef PARALLELstd::sort(policy, v.begin(), v.end());
#elsestd::sort(v.begin(), v.end());
#endifconst auto finish = std::chrono::steady_clock::now();std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start)<< '\n';
};/*** 辅助计时类*/
class Timer {std::string str;clock_t     start;public:Timer(const std::string& str) : str(str) {start = clock();}~Timer() {clock_t end = clock();std::cout << str << " => " << (end - start) / 1000.0 << '\n';}
};/*** 串行执行策略* class sequenced_policy;*/
void test_sequenced_policy(std::vector<int> arr) {Timer timer("std::execution::seq");std::sort(std::execution::seq, arr.begin(), arr.end());
}/*** 并行执行策略* class parallel_policy;*/
void test_parallel_policy(std::vector<int> arr) {Timer timer("std::execution::par");std::sort(std::execution::par, arr.begin(), arr.end());
}/*** 并行无序执行策略* class parallel_unsequenced_policy;*/
void test_parallel_unsequenced_policy(std::vector<int> arr) {Timer timer("std::execution::par_unseq");std::sort(std::execution::par_unseq, arr.begin(), arr.end());
}/*** 无序执行策略* class unsequenced_policy;*/
void test_unsequenced_policy(std::vector<int> arr) {
#if __cpp_lib_execution >= 201902LTimer timer("std::execution::unseq");std::sort(std::execution::unseq, arr.begin(), arr.end());
#endif
}int main()
{std::vector<std::uint64_t> v(1'000'000);std::mt19937 gen{ std::random_device{}() };std::ranges::generate(v, gen);//C++ 20 标准measure(execution::seq, v);measure(execution::unseq, v);measure(execution::par_unseq, v);measure(execution::par, v);std::cout << "__cplusplus=============================" << __cplusplus << "\n";std::vector<int> arr;for (int i = 0; i < 3.0 * 100'000'00; i += 1) {arr.push_back(rand());}test_sequenced_policy(arr);test_parallel_policy(arr);test_parallel_unsequenced_policy(arr);test_unsequenced_policy(arr);std::cout << "hello world\n";
}

运行结果:

参考:

https://zh.cppreference.com/w/cpp/algorithm/execution_policy_tag
https://blog.csdn.net/CUBE_lotus/article/details/137159423

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/52303.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

外排序之文件归并排序实现

外排序介绍 外排序是指能够处理极大量数据的排序算法。通常来说&#xff0c;外排序处理的数据不能一次装入内存&#xff0c;只能放在读写较慢的外存储器(通常是硬盘)上。外排序通常采用的是⼀种“排序-归并”的策略。在排序阶段&#xff0c;先读入能放在内存中的数据量&#x…

ACCESS 工具注入实战 凡诺靶场

简介 Access数据库注入攻击是一种常见的网络安全&#xff0c;通过注入SQL代码来获取未授权的数据访问权限。这种攻击利用了应用程序与数据库之间的交互漏洞&#xff0c;攻击者通过输入特定的SQL代码片段来操纵数据库查询&#xff0c;从而绕过应用程序的安全机制&#xff0c;获取…

基于x86 平台opencv的图像采集和seetaface6的人脸特征点功能

目录 一、概述二、环境要求2.1 硬件环境2.2 软件环境三、开发流程3.1 编写测试3.2 配置资源文件3.2 验证功能一、概述 本文档是针对x86 平台opencv的图像采集和seetaface6的人脸特征点功能,opencv通过摄像头采集视频图像,将采集的视频图像送给seetaface6的人脸特征点模块从而…

Selenium + Python 自动化测试19(补充-读取各种文件数据操作)

我们的目标是&#xff1a;按照这一套资料学习下来&#xff0c;大家可以独立完成自动化测试的任务。 上一篇我们讨论了数据驱动测试中如何完成重复的测试实例&#xff0c;今天我们补充一些读取各种文件的方法。 本篇文章我们讨论一下如何使用读取txt、CSV、Excel文件&#xff0…

多门店多端平台系统小程序源码

&#x1f525;【健身新纪元】多门店多端分开健身系统&#xff0c;打造你的个性化健身体验&#x1f31f; &#x1f3cb;️‍♀️ 一、告别单一&#xff0c;拥抱多元化健身时代 你还在为找不到合适的健身房而烦恼吗&#xff1f;或是想要随时随地开启健身模式&#xff0c;却受限…

xss靶场 pwnfunction WW3

目录 代码 代码分析 payload构造 结果 代码 <div><h4>Meme Code</h4><textarea class"form-control" id"meme-code" rows"4"></textarea><div id"notify"></div> </div><scri…

ubuntu上cmake3.30.2的安装

引言 安装下载安装包将安装包从windows拷贝到ubuntu解压进入解压后的文件夹执行boostrap编译CMake安装CMake查看是否安装成功 目前的ubuntu系统是20.04.4&#xff0c;用命令行安装了cmake的版本是3.16的&#xff0c;由于项目需要升级cmake到cmake3.22之上&#xff0c;使用命令行…

如何使用cornerstone3D渲染3D影像

&#x1f353; 前言 在日常开发中经常会遇到除了渲染MPR视图外&#xff0c;还需要渲染3D扫描影像&#xff0c;本文从具体的实现、viewport type解读、场景预设等方面来介绍3D具体的实现及涉及到的相关概念。 &#x1f951; 效果演示 点击查看完整代码 &#x1f352; 实现及概…

详细分析 el-progress的基本知识以及用法(附Demo)

目录 前言1. 基本知识2. Demo3. 实战 前言 由于实战项目中有所引用&#xff0c;对此记录基本的知识点&#xff0c;并且以Demo的形式呈现 1. 基本知识 el-progress 是 Element Plus UI 库中的一个进度条组件&#xff0c;用于显示任务的完成情况 可以帮助用户了解某个操作或任…

移动云电脑手机端3.0全新升级,畅享个人便捷管理,筑牢安全管控防线

在当今数字化飞速发展的时代&#xff0c;企业对于高效、便捷且安全的办公模式需求日益迫切。移动云电脑手机端3.0全新升级&#xff0c;迎来科技领域的又一突破。其基于企业管理平台为企业客户提供管理能力&#xff0c;实现对用户、终端、资源的全方位集中管控&#xff0c;助力提…

探索GitLab:从搭建到高效使用的实用指南

企业里为什么喜欢使用GitLab 一、GitLab简介二、搭建GitLab三、GitLab的权限管理3.1、用户注册3.2、创建用户组3.3、为用户组添加用户3.4、为工程添加访问权限 四、GitLab的code review五、团队知识管理六、总结 一、GitLab简介 GitLab是利用 Ruby on Rails 一个开源的版本管理…

Go开发桌面客户端软件小试:网站Sitemap生成

在前一篇【手把手教你用Go开发客户端软件&#xff08;使用Go HTML&#xff09;】中&#xff0c;我们详细介绍了如何通过Go语言开发一个简单的桌面客户端软件。本次&#xff0c;我们将继续这个系列&#xff0c;使用Go语言结合Sciter的Go绑定库——go-sciter&#xff0c;实战开发…

Linux虚拟机磁盘管理-新分区磁盘挂载

挂载mount 注意&#xff1a;挂载前一定要对磁盘进行文件系统创建&#xff0c;否则无法挂载磁盘 比如mkfs.ext4系统文件 磁盘挂载前和挂载后&#xff1a; linux系统重启后磁盘挂载没有了怎么办(不建议&#xff0c;哪怕输错一个字系统起不来&#xff0c;自己操作的时候出现过起不…

大语言模型-PDF文档解析

PDF解析能够提升大语言模型系统的信息处理能力和应用范围&#xff0c;为用户提供更加便捷、高效、个性化的服务体验。本文介绍三种常用的pdf解析方式&#xff1a;Open Parse、pdfplumber、PyMuPD。 一、Open Parse Open Parse是一个能够直观地识别文档布局并有效地对其进行分…

Qt 系统相关 - 网络与音视频

目录 一、Qt 网络 1. UDP Socket 1.1 核心 API 概览 1.2 回显服务器 1.3 回显客户端 2. TCP Socket 2.1 核心 API 概览 2.2 回显服务器 2.3 回显客户端 3. HTTP Client 3.1 核心 API 3.2 代码示例 二、Qt 音视频 1. Qt 音频 1.1 核心API概览 1.2 示例 2. Qt 视…

加速网络体验,Squid缓存代理:让浏览如飞,畅享无限网络速度!

作者简介&#xff1a;我是团团儿&#xff0c;是一名专注于云计算领域的专业创作者&#xff0c;感谢大家的关注 座右铭&#xff1a; 云端筑梦&#xff0c;数据为翼&#xff0c;探索无限可能&#xff0c;引领云计算新纪元 个人主页&#xff1a;团儿.-CSDN博客 目录 前言: squ…

订单到期关闭如何实现?

目录 一、被动关闭 二、定时任务 三、JDK自带的DelayQueue 四、Netty的时间轮 五、Kafka的时间轮 六、RocketMQ延迟消息 七、RabbitMQ死信队列 八、RabbitMQ插件 九、Redis过期监听 十、Redis的Zset 十一、Redisson 在电商、支付等系统中&#xff0c;一般都是先创建…

详解华为项目管理,附华为高级项目管理内训材料

&#xff08;一&#xff09;华为在项目管理中通过有效的沟通、灵活的组织结构、坚持不懈的努力、细致的管理和科学的考核体系&#xff0c;实现了持续的创新和发展。通过引进先进的管理模式&#xff0c;强调以客户需求为导向&#xff0c;华为不仅优化了技术管理和项目研发流程&a…

多重示例详细说明Eureka原理实践

Eureka原理&#xff08;Eureka Principle&#xff09;是指在长时间的思考和积累之后&#xff0c;通过偶然的瞬间获得灵感或发现解决问题的方法的一种认知现象。这个过程通常包括三个主要阶段&#xff1a;准备阶段、潜伏期以及突然的灵感爆发。下面详细说明Eureka原理的实践步骤…

vue3 组合式API

<!-- 深度监听 deep 点击按钮控制台&#xff0c;才输出count变化了: 1, 老值: 0;否则控制台不输出 --> <script setup>import { ref,watch } from vueconst state ref({count:0})const setCount () > {state.count.value}watch(state, () > {console.log(…