二进制转换为字符串
一个字节8位,拆分为两个4位字节(最大值16)
拆分后的字节映射到0123456789abcdef
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;
}
}
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;
}
#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;
}
//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();
}
}
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;
#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;
}
分析:
release版本优化的比较多,之所以C++17耗时较长原因是进入多线程次数远远大于C++11,C++11只进入了12次,而C++17采用lambda表达式函数,进入了TestNumber次