c++ rvo vs std::move
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://stackoverflow.com/questions/17473753/c11-return-value-optimization-or-move
c++ rvo vs std::move的更多相关文章
- C++ 11 右值引用以及std::move
转载请注明出处:http://blog.csdn.net/luotuo44/article/details/46779063 新类型: int和int&是什么?都是类型.int是整数类型,in ...
- Item 25: 对右值引用使用std::move,对universal引用则使用std::forward
本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 右值引用只能绑定那些有资格被move的对象上去.如 ...
- C++ 11中的右值引用以及std::move
看了很多篇文章,现在终于搞懂了C++ 中的右值以及std::move 左值和右值最重要的区别就是右值其实是一个临时的变量 在C++ 11中,也为右值引用增加了新语法,即&& 比 ...
- std::move()和std::forward()
std::move(t)负责将t的类型转换为右值引用,这种功能很有用,可以用在swap中,也可以用来解决完美转发. std::move()的源码如下 template<class _Ty> ...
- std::move()
#include <iostream> #include <utility> #include <vector> #include <string> i ...
- C++11右值引用和std::move语句实例解析
关键字:C++11,右值引用,rvalue,std::move,VS 2015 OS:Windows 10 右值引用(及其支持的Move语意和完美转发)是C++0x将要加入的最重大语言特性之一.从实践 ...
- c++ 11 移动语义、std::move 左值、右值、将亡值、纯右值、右值引用
为什么要用移动语义 先看看下面的代码 // rvalue_reference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #includ ...
- C++ 11 左值,右值,左值引用,右值引用,std::move, std::foward
这篇文章要介绍的内容和标题一致,关于C++ 11中的这几个特性网上介绍的文章很多,看了一些之后想把几个比较关键的点总结记录一下,文章比较长.给出了很多代码示例,都是编译运行测试过的,希望能用这些帮助理 ...
- C++11 std::move和std::forward
下文先从C++11引入的几个规则,如引用折叠.右值引用的特殊类型推断规则.static_cast的扩展功能说起,然后通过例子解析std::move和std::forward的推导解析过程,说明std: ...
随机推荐
- Application类-欢迎页(初始界面)
在程序界面显示前,如果我们处理了很多耗时操作,这个时候给用户提示一个欢迎页便是十分友好的.WPF为我们提供了这个特性: 第一种方法:通过设置图片资源的生成操作 粘贴一个图片到我们的项目中 在解决方案管 ...
- Bootstrap3-导航条
1. 定义导航条 <!-- 导航条 navbar --> <div class="navbar nav-bar-default"> <ul class ...
- python类的构造函数
最新的 Python3.7 中(2018.07.13),对类的构造函数进行了精简. from dataclasses import dataclass @dataclass class A: x:in ...
- Js判断flash是否被禁用,如果禁用并开启flash
<script> function flashChecker() { ; //是否安装了flash ; //flash版本 if(document.all) { var swf = new ...
- 本地eyoucms搬家
1.后台数据备份 2.删除install 里面的install.lock 3.清理缓存文件 data - runtime-删除所有文件: 4.项目中的文件全部压缩 即打包完毕:最后再把打包的文件放置到 ...
- 如何在相同的类名中单独为选中元素设置JS
很多时候,我发现对一个类名添加事件,每次都是所有同类名元素一起触发,使用 this可以仅对当前选中的元素应用事件 如 $('.guowai button').click(function() { /* ...
- maven 学习---Maven安装配置
想要安装 Apache Maven 在Windows 系统上, 只需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : JDK 1.8 ...
- 安装Ubuntu后需要做的事
卸载软件 纸牌 麻将 扫雷 数独 火狐 邮件 扫描 拍照 照片 视频 计划 日历 打印 备份 计算器 亚马逊 电源统计 音乐播放 远程桌面 To Do LibreOffice 换下载源 装机的时候下载 ...
- mysql系列2 权限相关
mysql授权认证 请注意(大坑):mysql8.0以前的版本可以使用grant在授权的时候隐式的创建用户,8.0以后已经不支持,所以必须先创建用户,然后再授权!! 例子: 在170mysql主机上授 ...
- c++产生验证码字符串
// // Created by lk on 18-10-14. // #include <iostream> #include <cstdlib> #include < ...