当前位置: 首页 > news >正文

长沙做网络推广的seo查询

长沙做网络推广的,seo查询,二维码付款跳转网页制作教程,上海网站备案在哪里查询什么是emplace_back emplace_back是C11引入的STL容器成员函数。emplace操作只执行构造而不执行拷贝构造。 如何理解上面这句话&#xff1f;先来看一个场景。 class test { public:test(){}test(int i){ std::cout << "test(int i)" << std::endl; }tes…

什么是emplace_back

emplace_back是C++11引入的STL容器成员函数。emplace操作只执行构造而不执行拷贝构造。
如何理解上面这句话?先来看一个场景。

class test
{
public:test(){}test(int i){ std::cout << "test(int i)" << std::endl; }test(const test& _test){ std::cout << "test(const test& _test)" << std::endl; }
};int main()
{std::vector<test> arr;arr.reserve(5);arr.push_back(1);arr.emplace_back(1);return 0;
}

test类显式写出了三个构造函数,分别是无参构造、单参数构造、拷贝构造。因为C++中单参数的构造函数支持隐式类型转换,因此可以拿一个整型构造一个test对象。除非用explicit修饰构造函数,表示禁止隐式类型转换构造。

test(int i)
test(const test& _test)
test(int i)

这是上述代码的运行结果,可以看到push_back中,会用整数1构造一个test对象,再通过拷贝构造再构造一个对象,这才是插入到arr数组中的对象。
而emplace_back只有一次隐式类型转换的构造,因为emplace是直接使用参数在容器管理的内存空间中直接构造元素,因此没有拷贝的操作,在一定程度上提高了效率。

写入大数据时的性能差异

class test
{
public:test(){}test(int i){ }test(const test& _test){ }
};void test_push_back()
{clock_t start = clock();std::vector<test> arr;arr.reserve(1000000000);for(int i = 0; i < 1000000000; i++){arr.push_back(1);}std::cout <<"耗时:"<< (clock() - start) << std::endl;/*耗时:24.41秒*/
}void test_emplace_back()
{clock_t start = clock();std::vector<test> arr;arr.reserve(1000000000);for(int i = 0; i < 1000000000; i++){arr.emplace_back(1);}std::cout <<"耗时:"<< (clock() - start) << std::endl;/*耗时:15.86秒*/
}int main()
{test_push_back();test_emplace_back();return 0;
}shell脚本用于实时监控进程运行状况
while true; do ps aux | head -1 ; ps aux | grep a.out; printf "——————————————————————\n\n" ; sleep 1; done

一个栈上的test对象的大小是1个字节,因为test类没有存储对象成员,而编译器为了表明一个test对象存在,需要用1字节作为占位符。
109 个对象即1亿个,1个对象是1个字节。后台检测到有982456个字节,即大约1GB。可以看到,插入109个test对象的时候,差距大概是10秒钟。

应用场景

你可能说,这种优化有什么用,有什么类会允许隐式类型转换,还可能插入的时候不显式构造?
没错,就是string。

void test_push_back()
{clock_t start = clock();std::vector<std::string> arr;arr.reserve(1000000000);for(int i = 0; i < 100000000; i++){arr.push_back("Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! ");}std::cout <<"耗时:"<< (clock() - start) << std::endl;/*耗时:25.66秒*/
}void test_emplace_back()
{clock_t start = clock();std::vector<std::string> arr;arr.reserve(1000000000);for(int i = 0; i < 100000000; i++){arr.emplace_back("Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! ");}std::cout <<"耗时:"<< (clock() - start) << std::endl;/*耗时:16.20秒*/
}
耗时:20.39秒
耗时:17.87

可是差距好像并不是很大=-=

综上所述:可能提升有限,但是能使用emplace_back还是不使用push_back了。

http://www.hengruixuexiao.com/news/25451.html

相关文章:

  • 广西柳州做网站怎么投放广告是最有效的
  • app运营模式seo优化排名怎么做
  • 医妃有毒禁欲王爷狠狠宠新网站百度seo如何做
  • 网站建设的流程怎么写百度推广账户登录首页
  • 成都建立网站数据分析软件哪个最好用
  • 新闻源网站做黑帽seo网页广告怎么做
  • 陕西住房与建设厅网站推广关键词如何优化
  • 网页设计与制作教程 刘瑞信 pdf肇庆seo按天计费
  • 海淀青岛网站建设如何创建自己的域名
  • 织梦系统网站地图模板下载专注于网站营销服务
  • 顶级域名是什么济南网络seo公司
  • 网站建设的例子长春网站建设平台
  • 余姚做百度网站建设职业培训学校
  • django做企业级网站佛山网站建设技术托管
  • 谷歌推广seo优化网站搜索排名
  • 什么网站是做汽车装饰配件的网络seo软件
  • 沈阳和平三好街做网站湖南正规关键词优化首选
  • 自己设计好的网站怎么设置访问公司地址怎么弄在百度上显示
  • 天津交友网站建设百度爱采购怎么优化排名
  • 前端做网站需要学什么网络营销活动方案
  • 网站设计文章产品推广的渠道
  • 佛山网站制作好处优化营商环境的措施建议
  • 买过域名之前就可以做网站了吗?在线科技成都网站推广公司
  • 北京疫情感染率seo网站关键词优化机构
  • 枣庄网站建设百度推广的价格表
  • 网站使用cookies正规电商培训班
  • 跨越网站建设科技有限公司四种基本营销模式
  • 江苏新宁建设集团网站网络运营需要学什么
  • 郑州网站优化哪家好网站seo排名优化工具在线
  • 盐城网站建设找宇联排名推广网站