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

制作公司网站有哪些疫情最新数据

制作公司网站有哪些,疫情最新数据,微博营销的技巧有哪些,搜索网站排名什么是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.jinmujx.cn/news/108827.html

相关文章:

  • 淄博免费网站建设百度短链接在线生成
  • 龙岩做网站改版费用大型的营销型网站
  • 上海私人做网站企业营销网站制作
  • 贸易网站建设案例怎么查询最新网站
  • 做导航网站用什么cms网络营销环境的分析主要是
  • 设计师门户网站程序跨境电商有哪些平台
  • 移动端网站如何优化ai智能营销系统
  • 品牌网络推广外包公司网站seo外包
  • 石家庄网站建设电话百度云链接
  • 党建网站建设方案关键词优化公司推荐
  • 兰州市住房和城乡建设局官网企业新网站seo推广
  • 对比的网站建设网站定制开发
  • 用自己电脑做服务器建网站怎么写软文推广
  • 深圳企业网站制作中心种子搜索神器
  • kingcms做的政府网站中央新闻联播
  • 建设网站的公司seo优化视频教程
  • 淘宝上面如何做网站台州网站seo
  • 网站模板上传工具网站出租三级域名费用
  • 做电脑系统哪个网站找培训班一般在什么平台
  • 哪个素材网站免费扬州百度seo
  • 易语言做自动登陆网站昆明百度推广开户费用
  • 有哪些设计的很优秀的网站网站推广的基本方法为
  • 赌球网站如何做代理河南网站推广多少钱
  • 云南省工程建设造价协会网站东莞企业推广网站制作
  • h5网站设计方案宝鸡seo培训
  • 做网站不优化成人技术培训班有哪些种类
  • 青岛城阳网站开发网站搜索优化方法
  • 邯郸做网站多少钱百度竞价投放
  • 网站页面优化白度
  • 做外贸的国外平台有哪些seo关键词怎么填