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 <iostream>
#include <chrono>
#include <unordered_map> 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 <int, std::string> 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:"<<tt<<std::endl;
return tt;
} std::string get_st_v1()
{
std::string st;
st = "ttppppppppppppppppppppppppppppppppppppppppppppppppppppppppp";
return st;
} std::string get_st_v2()
{
std::string st;
st = "ttppppppppppppppppppppppppppppppppppppppppppppppppppppppppp";
return std::move(st);
} info get_info_v1()
{
info ifo;
ifo.str = "ttppppppppppppppppppppppppppppppppppppppppppppppppppppppppp";
ifo.umap.insert(std::make_pair<int, std::string>(, "eggs"));
return ifo;
} info get_info_v2()
{
info ifo;
ifo.str = "ttppppppppppppppppppppppppppppppppppppppppppppppppppppppppp";
ifo.umap.insert(std::make_pair<int, std::string>(, "eggs"));
return std::move(ifo);
} BigObject foo(int n) { BigObject localObj;
return localObj;
} int main() {
auto f = foo(); int64_t t_1= get_current_time_ns(); std::cout<<"test rvo:"<<std::endl;
for(int i = ; i< ; i++)
{
std::string d1 = get_st_v1();
} int64_t t_2= get_current_time_ns(); std::cout<<"v1 time cost:"<<t_2-t_1<<std::endl; std::cout<<"test move:"<<std::endl;
for(int j = ; j< ; j++)
{
std::string d2 = get_st_v2();
}
int64_t t_3= get_current_time_ns(); std::cout<<"v2 time cost:"<<t_3-t_2<<std::endl; std::cout<<"info test rvo:"<<std::endl;
for(int m = ; m< ; m++)
{
info d3 = get_info_v1();
}
int64_t t_4= get_current_time_ns(); std::cout<<"info v1 time cost:"<<t_4-t_3<<std::endl; std::cout<<"info test move:"<<std::endl;
for(int n = ; n< ; n++)
{
info d4 = get_info_v2();
}
int64_t t_5= get_current_time_ns(); std::cout<<"info v2 time cost:"<<t_5-t_4<<std::endl; return ;
}

Result

constructor.
current time:
test rvo:
current time:
v1 time cost:
test move:
current time:
v2 time cost:
info test rvo:
current time:
info v1 time cost:
info test move:
current time:
info v2 time cost:
destructor.

Reference

https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en

https://stackoverflow.com/questions/17473753/c11-return-value-optimization-or-move

https://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion-return-statement

https://stackoverflow.com/questions/12011426/how-to-use-move-semantics-with-stdstring-during-function-return

c++ rvo vs std::move的更多相关文章

  1. C++ 11 右值引用以及std::move

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/46779063 新类型: int和int&是什么?都是类型.int是整数类型,in ...

  2. Item 25: 对右值引用使用std::move,对universal引用则使用std::forward

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 右值引用只能绑定那些有资格被move的对象上去.如 ...

  3. C++ 11中的右值引用以及std::move

    看了很多篇文章,现在终于搞懂了C++ 中的右值以及std::move   左值和右值最重要的区别就是右值其实是一个临时的变量 在C++ 11中,也为右值引用增加了新语法,即&&   比 ...

  4. std::move()和std::forward()

    std::move(t)负责将t的类型转换为右值引用,这种功能很有用,可以用在swap中,也可以用来解决完美转发. std::move()的源码如下 template<class _Ty> ...

  5. std::move()

    #include <iostream> #include <utility> #include <vector> #include <string> i ...

  6. C++11右值引用和std::move语句实例解析

    关键字:C++11,右值引用,rvalue,std::move,VS 2015 OS:Windows 10 右值引用(及其支持的Move语意和完美转发)是C++0x将要加入的最重大语言特性之一.从实践 ...

  7. c++ 11 移动语义、std::move 左值、右值、将亡值、纯右值、右值引用

    为什么要用移动语义 先看看下面的代码 // rvalue_reference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #includ ...

  8. C++ 11 左值,右值,左值引用,右值引用,std::move, std::foward

    这篇文章要介绍的内容和标题一致,关于C++ 11中的这几个特性网上介绍的文章很多,看了一些之后想把几个比较关键的点总结记录一下,文章比较长.给出了很多代码示例,都是编译运行测试过的,希望能用这些帮助理 ...

  9. C++11 std::move和std::forward

    下文先从C++11引入的几个规则,如引用折叠.右值引用的特殊类型推断规则.static_cast的扩展功能说起,然后通过例子解析std::move和std::forward的推导解析过程,说明std: ...

随机推荐

  1. 深入V8引擎-默认Platform之mac篇(2)

    先说结论,V8引擎在默认Platform中初始化的这个线程是用于处理类似于setTimeout的延时任务. 另外附一些图,包括继承树.关键属性归属.纯逻辑工作流程,对代码木得兴趣的看完图可以X掉了. ...

  2. 如何在windows 7 上使用docker

    在window7上面本来是支持使用docker的, 但是我们可以通过安装docker toolbox来实现在windows7上使用docker. 参考网址: https://docs.docker.c ...

  3. oracle中如何生成awr【性能调优】报告

    1.进入数据库 sqlplus / as sysdba 2.查看用户 show parameter db_name 3.开始压测后执行 exec DBMS_WORKLOAD_REPOSITORY.CR ...

  4. ES6 Class(类)(九)

    一.Class类1.构造函数 constructor(){} 说明:a.如果没有重新定义带参数的构造函数,类会默认为我们提供一个不带参数的隐式构造函数b.在创建类的实例时会自动调用类中的构造函数 2. ...

  5. 跨域漏洞丨JSONP和CORS跨域资源共享

    进入正文之前,我们先来解决个小问题,什么是跨域? 跨域:指的是浏览器不能执行其它网站的脚本,它是由浏览器的同源策略造成的,是浏览器的安全限制! 跨域常见的两种方式,分别是JSONP和CORS. 今天i ...

  6. set实现交集,并集,差集

    let a = new Set([1, 2, 3]); let b = new Set([4, 3, 2]); // 并集 let union = new Set([...a, ...b]); // ...

  7. WC.exe(Java实现)

    一.GitHub项目地址:https://github.com/nullcjm/mypage 二.项目相关要求: wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写 ...

  8. Yum项目上线实战 (网站运维)-Linux从入门到精通第十一天(非原创)

    文章大纲 一.编译安装与卸载Nginx二.关于LAMP三.LAMP环境部署四.学习资料下载五.参考文章   一.编译安装与卸载Nginx Nginx:是一款比较流行的web服务器软件,类似于Apach ...

  9. 如果再聘请一位会css的美工,那要你还有什么用?

    老板正为网站不好看而烦恼. 会议结束后. 我对我的前辈说:要是能找个会css美工就好了! 前辈说:如果再聘请一位会css的美工,那要你还有什么用? 我一时语塞. 后来回答:我可以专心后端代码,可以写 ...

  10. c# MVC5(一) 初步认识以及新建mvc

    一:MVC5初始 1:广义MVC(Model--View-Controller): V是界面 : M是数据和逻辑 : C是控制,把M和V链接起来: 是程序设计模式,一种设计理念,可以有效的分离界面和业 ...