如何让C++字符串更快
2023-03-07 09:09:30 时间
1、上代码,原有写法
#include <iostream>
#include <string>
static uint32_t s_AllocCount = 0;
void* operator new(size_t size)
{
s_AllocCount++;
std::cout << "Allocating " << size << " bytes\n";
return malloc(size);
}
void PrintName(const std::string& name)
{
std::cout << name << std::endl;
}
int main()
{
std::string name = "Yan Chernikov";
PrintName(name);
std::string firstName = name.substr(0, 3);
std::string lastName = name.substr(4, 9);
std::cout << s_AllocCount << " allocations" << std::endl;
std::cin.get();
}
1.1、运行结果
std::string name ="Yan Chernikov";
PrintName(name);
std::string firstName = name.substr(0,3);
std::string lastName = name.substr(4,9);
2、第一次优化
2.1、代码
#include <iostream>
#include <string>
static uint32_t s_AllocCount = 0;
void* operator new(size_t size)
{
s_AllocCount++;
std::cout << "Allocating " << size << " bytes\n";
return malloc(size);
}
void PrintName(std::string_view name)
{
std::cout << name << std::endl;
}
int main()
{
std::string name = "Yan Chernikov";
#if 0
std::string firstName = name.substr(0, 3);
std::string lastName = name.substr(4, 9);
#else
std::string_view firstName(name.c_str(), 3);
std::string_view lastName(name.c_str() + 4, 9);
#endif
PrintName(firstName);
PrintName(lastName);
std::cout << s_AllocCount << " allocations" << std::endl;
std::cin.get();
}
2.2 运行结果
2.3、用string_view 替换string
3、第二次优化
3.1、代码
#include <iostream>
#include <string>
static uint32_t s_AllocCount = 0;
void* operator new(size_t size)
{
s_AllocCount++;
std::cout << "Allocating " << size << " bytes\n";
return malloc(size);
}
void PrintName(std::string_view name)
{
std::cout << name << std::endl;
}
int main()
{
const char* name = "Yan Chernikov";
#if 0
std::string firstName = name.substr(0, 3);
std::string lastName = name.substr(4, 9);
#else
std::string_view firstName(name, 3);
std::string_view lastName(name + 4, 9);
#endif
PrintName("firstName");
PrintName("lastName");
std::cout << s_AllocCount << " allocations" << std::endl;
std::cin.get();
}
3.2、运行结果
3.3、修改的代码
const char* name = "Yan Chernikov";
std::string_view firstName(name, 3);
std::string_view lastName(name + 4, 9);
PrintName("firstName");
PrintName("lastName");
4、第二次修改(const char* name = "Yan Chernikov";)在只能使用string且数据量较大的情况下,是不方便的。
我们要做的是修改PrintName函数中的参数为PrintName(std::string_view nam),分配次数就可以从1变成0了。
4.1、代码
#include <iostream>
#include <string>
static uint32_t s_AllocCount = 0;
void* operator new(size_t size)
{
s_AllocCount++;
std::cout << "Allocating " << size << " bytes\n";
return malloc(size);
}
void PrintName(std::string_view name)
{
std::cout << name << std::endl;
}
int main()
{
const char* name = "Yan Chernikov";
#if 0
std::string firstName = name.substr(0, 3);
std::string lastName = name.substr(4, 9);
#else
std::string_view firstName(name, 3);
std::string_view lastName(name + 4, 9);
#endif
PrintName(name);
/*PrintName("firstName");
PrintName("lastName");*/
std::cout << s_AllocCount << " allocations" << std::endl;
std::cin.get();
}
4.2、运行结果:
5、总结
像这种在C++中,尤其是多个循环中,使用string是非常影响性能的,如果可以使用string_view,则可以有效提示效率,加快运行速度。
相关文章
- 飞天加速计划·高校学生在家实践·续费任务·使用体验
- Mysql 分组函数(多行处理函数),对一列数据求和、找出最大值、最小值、求一列平均值。
- 关于ECS高校计划的使用体验
- 关于阿里云ECS学生使用概述
- Mysql ifnull()空处理函数
- PF_RING实现分析(2)
- 智慧公安情报智能研判分析系统开发,合成作战平台建设
- VSCode 和 Anaconda python 开发
- 第一次使用阿里云云服务器的经历
- 智能合约有哪些特点?DAPP智能合约系统模式开发
- PF_RING实现分析(3)
- c语言实现n人围圈报数,报到3的人退出圈子,求最后留下的是几号位的人?
- PF_RING实现分析(1)
- Mybatis 强大的结果集映射器resultMap
- 飞天加速计划——高校学生在家实践续费任务文章
- ESC初使用体验——vsftp服务安装
- 国家质量基础设施一站式服务平台开发,NQI线上系统开发
- windows系统蓝牙功能突然消失怎么解决?
- 太高效了!玩了这么久的Linux,居然不知道这7个终端快捷键!
- 跳槽能解决多少问题?