博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ rvo vs std::move
阅读量:4955 次
发布时间:2019-06-12

本文共 1806 字,大约阅读时间需要 6 分钟。

c++ rvo vs std::move

To summarize, RVO is a compiler optimization technique, while std::move is just an rvalue cast, which also instructs the compiler that it's eligible to move the object. The price of moving is lower than copying but higher than RVO, so never apply std::move to local objects if they would otherwise be eligible for the RVO.

 

#include 
#include
#include
class BigObject {public: BigObject() { std::cout << "constructor. " << std::endl; } ~BigObject() { std::cout << "destructor."<< std::endl; } BigObject(const BigObject&) { std::cout << "copy constructor." << std::endl; } BigObject(BigObject&&) { std::cout << "move constructor"<< std::endl; }};struct info{ std::string str; std::unordered_map
umap;};int64_t get_current_time_ns(){ std::chrono::nanoseconds ss = std::chrono::high_resolution_clock::now().time_since_epoch(); int64_t tt = ss.count(); std::cout<<"current time:"<
<
(6, "eggs")); return ifo;}info get_info_v2(){ info ifo; ifo.str = "ttppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"; ifo.umap.insert(std::make_pair
(6, "eggs")); return std::move(ifo);}BigObject foo(int n) { BigObject localObj; return localObj;}int main() { auto f = foo(1); int64_t t_1= get_current_time_ns(); std::cout<<"test rvo:"<

 

Result

constructor. current time:1568273863513694551test rvo:current time:1568273863517139874v1 time cost:3445323test move:current time:1568273863521213442v2 time cost:4073568info test rvo:current time:1568273863574775754info v1 time cost:53562312info test move:current time:1568273863641223923info v2 time cost:66448169destructor.

 

Reference

转载于:https://www.cnblogs.com/pugang/p/11512501.html

你可能感兴趣的文章
maven package,clean,install,compile命令
查看>>
Python 排错UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法
查看>>
洛谷 P5276 模板题(uoi)
查看>>
ORACLE之PACKAGE-包、存储过程、函数
查看>>
select remove option safari 兼容
查看>>
常用的工具类所属的包
查看>>
设计模式-观察者模式(Observer Pattern)
查看>>
JVM 内部运行线程介绍
查看>>
2. DVWA亲测文件包含漏洞
查看>>
23.通过MS17_010来学习msf对渗透的利用
查看>>
PAT Basic 1073. 多选题常见计分法
查看>>
7-Java-C(四平方和)
查看>>
ORA-04052\ ORA-00604\ORA-12154
查看>>
【转载】Eclipse自动编译问题
查看>>
hdu 1166 敌兵布阵【线段树】(求给定区间和)
查看>>
HDU2255 奔小康赚大钱 (最大权完美匹配) 模板题【KM算法】
查看>>
CodeForces 510C Fox And Names (拓扑排序)
查看>>
x264
查看>>
ES 06 - 通过Kibana插件操作ES中的索引文档 (CRUD操作)
查看>>
1231作业
查看>>