C++11 – 使用std::codecvt进行字符编码转换需要注意的时间效率问题
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++11 – 使用std::codecvt进行字符编码转换需要注意的时间效率问题
原文链接:https://www.stubbornhuang.com/1867/
发布于:2021年12月21日 14:30:12
修改于:2021年12月21日 14:32:28
1 使用std::codecvt进行Unicode转UTF8字符编码的时间效率测试
通常情况下,使用std::codecvt进行Unicode转UTF8编码会使用以下的函数:
std::string Std_UnicodeToUTF8(const std::wstring& unicodeStr)
{
std::string ret;
try
{
std::wstring_convert< std::codecvt_utf8<wchar_t> > unicode_to_utf8_conv;
ret = unicode_to_utf8_conv.to_bytes(unicodeStr);
}
catch (const std::exception& e)
{
}
return ret;
}
我所使用的测试时间效率的代码如下:
#include <iostream>
#include <string>
#include <codecvt>
#include <chrono>
std::string Std_UnicodeToUTF8(const std::wstring& unicodeStr)
{
std::string ret;
try
{
std::wstring_convert< std::codecvt_utf8<wchar_t> > unicode_to_utf8_conv;
ret = unicode_to_utf8_conv.to_bytes(unicodeStr);
}
catch (const std::exception& e)
{
}
return ret;
}
int main()
{
auto beforeTime = std::chrono::steady_clock::now();
std::wstring unicodeStr = L"我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国";
for (int i = 0; i < 5000; ++i)
{
std::string utf8 = Std_UnicodeToUTF8(unicodeStr);
}
auto afterTime = std::chrono::steady_clock::now();
double duration_millsecond = std::chrono::duration<double, std::milli>(afterTime - beforeTime).count();
std::cout << "Unicode转utf8耗时:" << duration_millsecond << "毫秒" << std::endl;
}
做5000次Unicode编码转UTF8编码字符串转换,上述代码在测试机上Debug耗时:944.763ms,Release耗时167.571ms。这个结果出来,我人都麻了,这个转换效率太慢了。
2 编码转换时间效率低的原因
经过观察代码我们发现,每调用一次Std_UnicodeToUTF8函数就要新建一个std::wstring_convert< std::codecvt_utf8
3 换成static对象后的测试代码
#include <iostream>
#include <string>
#include <codecvt>
#include <chrono>
std::string Std_UnicodeToUTF8(const std::wstring& unicodeStr)
{
std::string ret;
try
{
static std::wstring_convert< std::codecvt_utf8<wchar_t> > unicode_to_utf8_conv;
ret = unicode_to_utf8_conv.to_bytes(unicodeStr);
}
catch (const std::exception& e)
{
}
return ret;
}
int main()
{
auto beforeTime = std::chrono::steady_clock::now();
std::wstring unicodeStr = L"我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国我爱你中国";
for (int i = 0; i < 5000; ++i)
{
std::string utf8 = Std_UnicodeToUTF8(unicodeStr);
}
auto afterTime = std::chrono::steady_clock::now();
double duration_millsecond = std::chrono::duration<double, std::milli>(afterTime - beforeTime).count();
std::cout << "Unicode转utf8耗时:" << duration_millsecond << "毫秒" << std::endl;
}
做5000次Unicode编码转UTF8编码字符串转换,上述代码在测试机上Debug耗时:104.362ms,Release耗时7.236ms。经过测试结果上看,修改为静态对象,在每次调用Std_UnicodeToUTF8函数时都是调用静态对象,而不是重新创建,这节省了巨大的时间开销。
对于高并发C++服务器,一般http请求为了节省带宽都是使用utf8编码,而C++业务服务器内部为国际化或者有处理中文字符串的需求,一般情况下会使用Unicode编码,所以在每一次请求时都需要进行UTF8->Unicode,Unicode->UTF8的转换,这就要求这个转换过程必须要快速。所以,请合理使用std::codecvt。
当前分类随机文章推荐
- C++ - const修饰符与指针 阅读375次,点赞1次
- C++STL容器 - std::map删除指定元素 阅读2130次,点赞0次
- C++11 - std::function简要介绍以及可包装函数的几种形式总结 阅读3218次,点赞0次
- C++读取Shp文件并将Shp转化为DXF 阅读3207次,点赞1次
- C++11 - 构建一个符合实际应用要求的线程池 阅读1186次,点赞0次
- C++ - queue存储动态指针时正确释放内存 阅读5555次,点赞2次
- C++ – UTF8编码下的全角字符转半角字符 阅读1822次,点赞0次
- C++ - sleep睡眠函数总结 阅读1116次,点赞0次
- C++ - 判断两个字符串是否相等方法总结 阅读423次,点赞0次
- C++ - 在Windows/Linux上创建单级目录以及多级目录的跨平台方法 阅读1156次,点赞0次
全站随机文章推荐
- 资源分享 - VR Developer Gems 英文高清PDF下载 阅读1564次,点赞1次
- 人工智能 - YOLO v3,YOLO v4,YOLO v5等版本演变史 阅读1749次,点赞1次
- OpenCV - 静态图片人脸检测和摄像头人脸检测 阅读3162次,点赞0次
- 三维旋转 - 四元数的基本概念以及四元数与欧拉角、旋转矩阵的相互转换 阅读981次,点赞1次
- Python - 获取当前时间字符串 阅读1068次,点赞0次
- OpenCV - 读取一个图像,并使用Canny算子进行边缘提取 阅读3079次,点赞0次
- WordPress - 自定义修改用户登录页面的logo(图片、链接、提示信息)、登录框内容、底部内容、css 阅读87次,点赞0次
- Modern OpenGL从零开始 - Fbxsdk::FbxAMatrix转换为glm::mat4 阅读2592次,点赞0次
- 资源分享 - GPU Pro 360 - Guide to Image Space 英文高清PDF下载 阅读2046次,点赞1次
- 我的项目 - Windows/Linux动态库加载类 阅读3283次,点赞0次
评论
169