• C++多线程编程(第四章 案例1,C++11和C++17 多核并行计算样例)


    4.1手动实现多核base16编码

    4.1.1 实现base16编码

    二进制转换为字符串
    一个字节8位,拆分为两个4位字节(最大值16)
    拆分后的字节映射到0123456789abcdef
    
    
    • 1
    • 2
    • 3
    • 4
    4.1.1.1 编码16进制
    void Base16Encode(const unsigned char* data, int size, unsigned char* out)
    {
    	for (int i = 0; i < size; i++)
    	{
    		unsigned char d = data[i];
    		//0000 0000
    		//1234 5678 >> 4              => 0000 1234 移位操作
    		//1234 5678 & 0000 1111       => 0000 5678
    		char a = base16[d >> 4];//取出高位
    		char b = base16[d & 0x0F];//取出低位
    		out[i * 2] = a;
    		out[i * 2+1] = b;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    4.1.1.2 反解码16进制
    
    static const char base16[] = "0123456789abcdef";
    
    
    unsigned char ch2hex(char ch)
    {
    	for (unsigned char i = 0; i != 16; ++i)
    		if (ch == base16[i])
    			return i;
    	return 0;
    }
    
    char* HEX2Char(const char* src)
    {
    	int i = 0;
    	int cnt = 0;
    	char*d = new char[strlen(src)];//先开辟空间多一点
    
    	while (*src)
    	{
    		if (i & 1)
    		{
    			//d[cnt++] |= ch2hex(*src);
    			//改编上面一行代码
    			d[cnt] = d[cnt] | ch2hex(*src);
    			cnt++;
    		}
    		else
    		{
    			d[cnt] = ch2hex(*src) << 4;
    		}
    		src++;
    		i++;
    	}
    
    	printf("cnt:%d\n", cnt);
    	d[cnt] = '\0';
    
    	return d;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    4.1.2无多线程代码

    
    #include 
    #include
    #include
    #include 
    #include 
    #include //计时头,C++11
    
    
    using namespace std;
    using namespace chrono;
    
    static const char base16[] = "0123456789abcdef";
    
    unsigned char ch2hex(char ch)
    {//字符解码
    	//static const char *hex = "0123456789ABCDEF";
    	for (unsigned char i = 0; i != 16; ++i)
    		//if (ch == hex[i])
    		if (ch == base16[i])
    			return i;
    	return 0;
    }
    
    char* HEX2Char(const char* src)
    {//字符串解码
    	int i = 0;
    	int cnt = 0;
    	char*d = new char[strlen(src)];//先开辟空间多一点
    
    	while (*src)
    	{
    		if (i & 1)
    		{
    			//d[cnt++] |= ch2hex(*src);
    			//改编上面一行代码
    			d[cnt] = d[cnt] | ch2hex(*src);
    			cnt++;
    		}
    		else
    		{
    			d[cnt] = ch2hex(*src) << 4;
    		}
    		src++;
    		i++;
    	}
    
    	printf("cnt:%d\n", cnt);
    	d[cnt] = '\0';
    
    	return d;
    
    }
    
    void Base16Encode(const unsigned char* data, int size, unsigned char* out)
    {
    	for (int i = 0; i < size; i++)
    	{
    		unsigned char d = data[i];
    		//0000 0000
    		//1234 5678 >> 4              => 0000 1234 移位操作
    		//1234 5678 & 0000 1111       => 0000 5678
    		char a = base16[d >> 4];//取出高位
    		char b = base16[d & 0x0F];//取出低位
    		out[i * 2] = a;
    		out[i * 2+1] = b;
    	}
    }
    
    
    int main()
    {
    	//使用字符串进行16位编码测试
    	string test_data = "测试base16编码";
    	printf("test_data size=%d\n", test_data.size());//一个中文占用2个字节,一个英文占用1个字节,如果要解码出来包含中文和英文,有点难度
    	unsigned char* out = new unsigned char[test_data.size()*2+1];
    	Base16Encode((unsigned char*)test_data.data(), test_data.size(), out);//编码
    	out[test_data.size()*2] = '\0';//字符串\0结尾,否则后面总有乱码
    	cout << "base16:" << out << endl;
    	printf("Decode:%s\n", HEX2Char((char*)out));//解码
    
    	//测试单线程base16编码效率
    	{
    		vector<unsigned char>in_data;
    		in_data.resize(1024 * 1024 * 20);//  1024*1024*10  -> 10M 测试数据
    		for (int i = 0; i < in_data.size(); i++)
    		{//初始化里面数据
    			in_data[i] = i % 256;
    
    		}
    		vector<unsigned char>out_data;
    		out_data.resize(in_data.size()*2);//是输入数据2倍
    		auto start = system_clock::now();//计时开始
    		Base16Encode(in_data.data(), in_data.size(),out_data.data());
    		auto end = system_clock::now();//计时结束
    		auto duration = duration_cast<milliseconds>(end-start);//模板函数,转换到毫秒
    		cout << "编码:" << in_data.size() << "字节数据花费" << duration.count() << "毫秒" << endl;
    
    		//cout << out_data.data() << endl;
    
    	
    	}
    
    	printf("All done!\n");
    
    	getchar();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    在这里插入图片描述

    4.1.3 C++ 11多线程代码

    //C++11多线程
    void Base16EncodeThread(const vector<unsigned char>&data, vector<unsigned char> &out)
    {
    	long size = data.size();
    	int th_count = thread::hardware_concurrency();//系统支持的线程核心数
    	printf("CPU report thread count:%d\n",th_count);
    
    	//对原始数据进行切片
    	long slice_count = size / th_count;//余数丢弃了,余数后续单独处理
    	if (size < th_count)
    	{//只切一片
    		th_count = 1;//1个线程搞定
    		slice_count = size;
    	}
    
    	vector<thread> ths;//准备好线程
    	ths.resize(th_count);
    
    
    	//任务分配到各个线程   
    	for (int i = 0; i < th_count; i++)
    	{
    		//eg. 1234 5678 9abc defg hi
    		//计算偏移位置
    		long offset = i * slice_count;
    		long count = slice_count;
    
    
    		//最后一个线程要把余数加起来一起处理
    		if (th_count > 1 && i == th_count - 1)
    		{
    			count = slice_count + size%th_count;
    		}
    		//cout << offset << ":" << count << endl;
    		ths[i] = thread(Base16Encode, data.data() + offset,count, out.data());
    	}
    
    	//等待所有线程处理结束
    	for (auto &th : ths)
    	{
    		th.join();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    4.1.4 C++ 17多线程并发

    		printf("C++17多线程Base16编码效率测试(编译的时候先检查设置C++17) 开始计算===========================\n");
    		//设置C++17方法:属性->C/C++ ->C++语言标准  ,设置ISOC++17
    		vector<unsigned char>in_data;
    		in_data.resize(TestNumber);//  1024*1024*10  -> 10M 测试数据
    		for (int i = 0; i < in_data.size(); i++)
    		{//初始化里面数据
    			in_data[i] = i % 256;
    
    		}
    		vector<unsigned char>out_data;
    		out_data.resize(in_data.size() * 2);//是输入数据2倍
    		auto start = system_clock::now();//计时开始
    		//#include  //C++17 支持
    		std::for_each(std::execution::par,//并行计算 多核
    			in_data.begin(),in_data.end(),
    			[&](auto& d)//多线程进入此函数
    			{
    				char a = base16[(d >> 4)];
    				char b = base16[(d & 0x0F)];
    				int index = &d - in_data.data();
    				out_data[index * 2] = a;
    				out_data[index * 2 + 1] = b;
    
    			}
    		);
    
    		auto end = system_clock::now();//计时结束
    		auto duration = duration_cast<milliseconds>(end - start);//模板函数,转换到毫秒
    		cout << "C++17多线程 编码:" << in_data.size() << "字节数据花费" << duration.count() << "毫秒" << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    4.1.5 所有测试代码汇总

    
    #include 
    #include
    #include
    #include 
    #include 
    #include //计时头,C++11
    
    #include  //C++17 for_each
    
    
    using namespace std;
    using namespace chrono;
    
    static const char base16[] = "0123456789abcdef";
    void Base16Encode(const unsigned char* data, long size, unsigned char* out)
    {
    	for (int i = 0; i < size; i++)
    	{
    		unsigned char d = data[i];
    		//0000 0000
    		//1234 5678 >> 4              => 0000 1234 移位操作
    		//1234 5678 & 0000 1111       => 0000 5678
    		char a = base16[d >> 4];//取出高位
    		char b = base16[d & 0x0F];//取出低位
    		out[i * 2] = a;
    		out[i * 2+1] = b;
    	}
    }
    
    
    //C++11多线程
    void Base16EncodeThread(const vector<unsigned char>&data, vector<unsigned char> &out)
    {
    	long size = data.size();
    	int th_count = thread::hardware_concurrency();//系统支持的线程核心数
    	printf("CPU report thread count:%d\n",th_count);
    
    	//对原始数据进行切片
    	long slice_count = size / th_count;//余数丢弃了,余数后续单独处理
    	if (size < th_count)
    	{//只切一片
    		th_count = 1;//1个线程搞定
    		slice_count = size;
    	}
    
    	vector<thread> ths;//准备好线程
    	ths.resize(th_count);
    
    
    	//任务分配到各个线程   
    	for (int i = 0; i < th_count; i++)
    	{
    		//eg. 1234 5678 9abc defg hi
    		//计算偏移位置
    		long offset = i * slice_count;
    		long count = slice_count;
    
    
    		//最后一个线程要把余数加起来一起处理
    		if (th_count > 1 && i == th_count - 1)
    		{
    			count = slice_count + size%th_count;
    		}
    		//cout << offset << ":" << count << endl;
    		ths[i] = thread(Base16Encode, data.data() + offset,count, out.data());
    	}
    
    	//等待所有线程处理结束
    	for (auto &th : ths)
    	{
    		th.join();
    	}
    }
    
    int main()
    {
    	int TestNumber = 1024 * 1024 * 20 - 1; //1024 * 1024 * 10  -> 10M 测试数据大小
    	//使用字符串进行16位编码测试
    	string test_data = "测试base16编码";
    	printf("test_data size=%d\n", test_data.size());//一个中文占用2个字节,一个英文占用1个字节,如果要解码出来包含中文和英文,有点难度
    	unsigned char* out = new unsigned char[test_data.size()*2+1];
    	Base16Encode((unsigned char*)test_data.data(), test_data.size(), out);//编码
    	out[test_data.size()*2] = '\0';
    	cout << "base16:" << out << endl;
    
    
    	//测试单线程base16编码效率
    	{
    		printf("单线程Base16编码效率测试 开始计算===========================\n");
    		vector<unsigned char>in_data;
    		in_data.resize(TestNumber);//  1024*1024*10  -> 10M 测试数据
    		for (int i = 0; i < in_data.size(); i++)
    		{//初始化里面数据
    			in_data[i] = i % 256;
    
    		}
    		vector<unsigned char>out_data;
    		out_data.resize(in_data.size()*2);//是输入数据2倍
    		auto start = system_clock::now();//计时开始
    		Base16Encode(in_data.data(), in_data.size(),out_data.data());
    		auto end = system_clock::now();//计时结束
    		auto duration = duration_cast<milliseconds>(end-start);//模板函数,转换到毫秒
    		cout << "编码:" << in_data.size() << "字节数据花费" << duration.count() << "毫秒" << endl;
    
    		//cout << out_data.data() << endl;//预览编码后的文本
    
    	}
    
    	//测试C++11多线程base16编码效率
    	{
    		printf("C++11多线程Base16编码效率测试 开始计算===========================\n");
    		vector<unsigned char>in_data;
    
    		in_data.resize(TestNumber);//  1024*1024*10  -> 10M 测试数据
    		for (int i = 0; i < in_data.size(); i++)
    		{//初始化里面数据
    			in_data[i] = i % 256;
    
    		}
    		vector<unsigned char>out_data;
    		out_data.resize(in_data.size() * 2);//是输入数据2倍
    		auto start = system_clock::now();//计时开始
    		Base16EncodeThread(in_data, out_data);//多线程
    		auto end = system_clock::now();//计时结束
    		auto duration = duration_cast<milliseconds>(end - start);//模板函数,转换到毫秒
    		cout << "C++11多线程 编码:" << in_data.size() << "字节数据花费" << duration.count() << "毫秒" << endl;
    
    		//cout << out_data.data() << endl;//预览编码后的文本
    
    	}
    
    	//测试C++11多线程base16编码效率
    	{
    		printf("C++17多线程Base16编码效率测试(编译的时候先检查设置C++17) 开始计算===========================\n");
    		//设置C++17方法:属性->C/C++ ->C++语言标准  ,设置ISOC++17
    		vector<unsigned char>in_data;
    		in_data.resize(TestNumber);//  1024*1024*10  -> 10M 测试数据
    		for (int i = 0; i < in_data.size(); i++)
    		{//初始化里面数据
    			in_data[i] = i % 256;
    
    		}
    		vector<unsigned char>out_data;
    		out_data.resize(in_data.size() * 2);//是输入数据2倍
    		auto start = system_clock::now();//计时开始
    		//#include  //C++17 支持
    		std::for_each(std::execution::par,//并行计算 多核
    			in_data.begin(),in_data.end(),
    			[&](auto& d)//多线程进入此函数
    			{
    				char a = base16[(d >> 4)];
    				char b = base16[(d & 0x0F)];
    				int index = &d - in_data.data();
    				out_data[index * 2] = a;
    				out_data[index * 2 + 1] = b;
    
    			}
    		);
    
    		auto end = system_clock::now();//计时结束
    		auto duration = duration_cast<milliseconds>(end - start);//模板函数,转换到毫秒
    		cout << "C++17多线程 编码:" << in_data.size() << "字节数据花费" << duration.count() << "毫秒" << endl;
    
    		//cout << out_data.data() << endl;//预览编码后的文本
    
    	}
    
    	printf("All done!\n");
    
    	getchar();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173

    在这里插入图片描述
    分析:
    release版本优化的比较多,之所以C++17耗时较长原因是进入多线程次数远远大于C++11,C++11只进入了12次,而C++17采用lambda表达式函数,进入了TestNumber次

  • 相关阅读:
    Java攻略之API
    Vue的MVVM实现原理
    redis无法远程连接的所有解决方案大全
    Java最强大的技术之一:反射
    自动驾驶争议不断,距离落地还有几道坎?|上云那些事
    LeetCode 1726. 同积元组:哈希表(组合数学)
    塑料透光率测试可测试塑料部件的透明度和纯度
    力扣第1005题 K 次取反后最大化的数组和 c++ 贪心 双思维
    8年经验之谈 —— 如何使用自动化工具编写测试用例?
    深度解析Shopee虾皮高效选品逻辑方法
  • 原文地址:https://blog.csdn.net/weixin_42727069/article/details/133853776