c++ rvo vs std::move

2023-05-16

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>(6, "eggs"));
    return ifo;
}

info get_info_v2()
{
    info ifo;
    ifo.str = "ttppppppppppppppppppppppppppppppppppppppppppppppppppppppppp";
    ifo.umap.insert(std::make_pair<int, std::string>(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:"<<std::endl;
    for(int i = 0; i< 100000; 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 = 0; j< 100000; 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 = 0; m< 100000; 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 = 0; n< 100000; 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 0;
}  

 

Result


constructor. 
current time:1568273863513694551
test rvo:
current time:1568273863517139874
v1 time cost:3445323
test move:
current time:1568273863521213442
v2 time cost:4073568
info test rvo:
current time:1568273863574775754
info v1 time cost:53562312
info test move:
current time:1568273863641223923
info v2 time cost:66448169
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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 如何修复 C++ 中的“对 Array::Array(unsigned long) 的未定义引用”? [复制]

    这个问题在这里已经有答案了 我有一个用于对 char 数组进行排序的类 其中一个构造函数采用参数 size t 长度 当我向它传递 int 类型的长度并尝试编译时 出现错误 driver cpp text 0x2c 对 Array Arra
  • 从方法链中使用的临时移出

    我正在尝试做类似的事情 include
  • Gradle compileKotlin includeRuntime 未将运行时添加到 jar

    我有一个 Kotlin Gradle 项目 我想在 jar 文件中包含 Kotlin 的运行时和 stdlib 我目前正在使用它 但当我使用 build gradle 配置构建项目时 它不包括运行时或 stdlib compileKotli
  • C++ std regex问号问题

    我在使用 std 正则表达式时遇到了麻烦 我无法使问号量词起作用 对 regex match 的调用将始终返回 0 我还尝试了 0 1 它的行为也不像我预期的那样 它的行为就像 量词 这是我的代码 include
  • 是否可以按值删除队列元素?

    我想从队列中删除具有特定值的元素 这样的事该怎么办呢 我正在尝试创建映射和队列的并发混合 目前我尝试在这个答案 https stackoverflow com questions 7704526 is thare in stl or boo
  • 如何在 C# 中为控件添加移动效果?

    我的 C 表单中有一个面板 并且有一个按钮 当我单击按钮时 会显示不可见的面板 相反 我希望面板移入或滑入 例如 当您单击组合框时 下拉列表不会弹出 我希望我的面板像那样显示 我怎样才能做到这一点 窗口动画是 Windows 的内置功能 这
  • 原始类型的完美转发

    我需要实现一些类似的方法vector emplace对于我自己的数据结构 在一般情况下 我会实现它们 以便它们支持完美转发 即使用右值引用 std forward还有那些东西 但是 如果我知道要转发的所有参数都是原始类型 例如int or
  • Visual C++ 2010 在调试时拒绝显示 std::string 值。显示<错误指针>

    我有一种奇怪的感觉 就像这是最近出现的问题 并且发生在两台不同的计算机上 当我调试并尝试查看 STL 中的 std string 的值时 它显示为值 它说它的大小是15 容量是一些乱码的巨大数字 数组值本身都显示 CXX0030 错误 无法
  • 如何散列 std::string?

    我正在制作一个小实用程序来帮助我通过重复来记住密码 我想每天只输入一次密码 而不是在每次会话之前输入 当然 我不会存储密码本身 但很乐意存储其哈希值 那么 获取哈希值的最简单方法是什么std string使用 C 标准库 对于不涉及外部库的
  • 为什么 std::type_info 不可复制?我可以把它存放在某个地方吗?

    The std type info类是不可复制的 这使得很难将其存储在对象中以供以后使用 我应该怎么办 C 11 中有一个更好的解决方案 一个名为 std type index 的新可复制包装器 您需要包含标头 typeindex 才能使用
  • C++ TR2 文件系统库的状态如何?

    截至上次布里斯托尔会议 C TR2 文件系统库的状态如何 它将成为 C 1Y C 14 的一部分 还是暂停 或者最近三次会议有任何已知的评论吗 It has 最近获得ISO委员会一致批准 http article gmane org gma
  • 从 boost::shared_ptr 转换为 std::shared_ptr?

    我有一个内部使用 Boost 版本的库shared ptr并且只暴露那些 对于我的应用程序 我想使用std shared ptr只要有可能 遗憾的是 这两种类型之间没有直接转换 因为引用计数的内容取决于实现 有什么办法可以同时拥有boost
  • 在 C++ 中检索 std::map 的随机关键元素

    如何在 C 中获取 std map 的随机密钥 使用迭代器 我不想维护额外的数据结构 std map迭代器是双向的 这意味着选择一个随机密钥将是O n 在不使用其他数据结构的情况下 基本上你唯一的选择就是使用std advance随机增量b
  • STL容器的范围插入函数在c ++ 11下返回void?

    刚刚学习c 所以我可能没有正确理解这一点 但我只读到范围插入函数在新标准下返回一个迭代器 C Primer 5th Ed cplusplus com http www cplusplus com reference string basic
  • 如何通过按住Java中的JPanel来移动未修饰的JFrame? [复制]

    这个问题在这里已经有答案了 到目前为止 我一直在制作一个未装饰的 JFrame 我想知道是否可以通过按住 JPanel 上的单击来移动未装饰的 JFrame 这是我正在处理的源代码 private static void createFra
  • std::condition_variable::wait 带谓词

    在 std condition variable 的文档中 存在以谓词函数作为参数的 wait 重载 该函数将等待 直到谓词函数为 true 的第一次wake up In the 文档 http en cppreference com w
  • 如何只匹配那些前面有偶数个“%”的数字?

    我想捕获字符串中任何位置出现的数字 并将其替换为 但我只想捕获那些具有偶数个的数字 在他们前面 不用担心周围的字符是否被捕获 我们可以使用捕获组来过滤掉数字 我无法想出 ECMAscript 正则表达式 这是操场 abcd 1 2 3 4
  • 为什么我收到 string does not name a type 错误?

    game cpp include
  • 如何使用 CUDA/Thrust 对两个数组/向量根据其中一个数组中的值进行排序

    这是一个关于编程的概念问题 总而言之 我有两个数组 向量 我需要对一个数组 向量进行排序 并将更改传播到另一个数组 向量中 这样 如果我对 arrayOne 进行排序 则对于排序中的每个交换 arrayTwo 也会发生同样的情况 现在 我知
  • 从空缓冲区构造“std::ostream”是否有效?

    考虑以下 std ostream out nullptr 这是合法且明确定义的吗 如果我现在这样做怎么样 out lt lt hello world n 这是合法且明确定义的吗 如果是这样 大概这是一种无操作 是的 实例化该流是合法且定义明

随机推荐

  • 浅谈UML学习笔记动态模型之序列图、协作图

    1 序列图 序列图和协作图都是交互图 xff0c 彼此等价 xff0c 可以相互转化 序列图是对对象之间传送消息的时间顺序的可视化表示 序列图用于表现交互 xff0c 侧重于强调时间顺序 序列图将交互关系表示为一个二维图 xff0c 如下图
  • No valid host was found. There are not enough hosts available

    root 64 dell PowerEdge T30 nova boot flavor m1 tiny image cirros nic net id 61 c2943fac a910 4cf6 b021 e8ab321965c9 secu
  • 校园网频繁断线、连不上网etc.

    这篇记录一下 Windows 下安装完 虚拟机 之后 xff0c 校园网 xff08 和其他网络 xff09 断线 xff0c 甚至无法连接到网络的情况 背景 xff1a 新装的系统 xff0c 网卡驱动什么的也都是刚装好的 xff0c 虚
  • JavaScript 笔记(3) -- JSON

    JavaScript JSON JavaScript Object Notation 是一种轻量级的数据交换格式 JSON 是用于存储和传输数据的格式JSON 通常用于服务端向网页传递数据 JSON 使用 JavaScript 语法 xff
  • 【转载】Notepad++使用技巧

    一 安装notepad 43 43 notepad 43 43 的下载 安装非常easy 下一步下一步 xff0c 所有选项都默认就可以安装好 但有几点需要注意 截止到写这篇博文 xff0c notepad 43 43 的最新版本为7 5
  • 0.1+0.2为什么不等于0.3

    首先 xff0c 对于不同的进制数值系统 xff0c 分母为多少时能除干净 xff1f 答案是当以前进制数的质因子为分母时 xff0c 以十进制为例 xff0c 它的质因子为2 5 xff0c 因此1 2 1 4 1 5 1 8和 1 10
  • git tag

    git tag的用法 git的tag功能 git 下打标签其实有2种情况 轻量级的 xff1a 它其实是一个独立的分支 或者说是一个不可变的分支 指向特定提交对象的引用带附注的 xff1a 实际上是存储在仓库中的一个独立对象 xff0c 它
  • 对web前端这门课程的期望

    对于这门课程 xff0c 我只希望我能过就行 xff0c 因为我将来可能不会从事着方面的工作 xff0c 但为了丰富自己的知识 xff0c 我还是觉得要认真的对待每一门学科 xff0c 这一门也不例外 xff0c 我希望我可以学的尽量好一点
  • ArcEngine安装并注册后应用程序无法使用toc等控件的解决办法

    安装了ArcEngine xff0c 并且用ecp注册过 但当放置toolbarcontrol TOCControl等控件在窗体上时 xff0c 提示 this control require an esri designer licens
  • Debian中安装使用sudo命令

    Debian中安装使用sudo命令 sudo可以让非root用户具有管理员的权限 xff0c 安装好的Debian后还不能使用sudo 需要使用root用户登陆后安装sudo命令 span style color 000102 span s
  • linux breakpad 编译,linux 平台编译googlebreakpad并测试 demo

    Linux googlebreakpad 编译 1 下载源码 源码包括两部分 xff0c 分为依赖库和 breakpad xff0c 网址一般会被屏蔽 xff0c 需要墙一下 另 xff0c 编译器需要支持 c 43 43 11 我用的gc
  • 233

    include lt bits stdc 43 43 h gt define reg register int define il inline define fi first define se second define mk a b
  • 再探容斥好题——ROOK

    这个时候考过 xff1a 安师大附中集训 Day2 当时看shadowice1984的做法 xff0c 但是没有亲自写 xff0c xff0c xff0c 雅礼集训考试的时候鼓捣半天 xff0c 被卡常到80pts xff0c 要跑9s 卡
  • CF908G New Year and Original Order

    CF908G New Year and Original Order gzz讲过 xff0c 可我到今天还是不会 有点trick的数位DP 比较显然的思路是 xff0c 考虑所有数中排序后每一位的贡献 cnt i x 表示S 1 S x 第
  • 本地在不安装Oracle的情况下安装PLSQL客户端

    本文解决问题 xff1a 通常在本地安装PLSQL后 xff0c 如果本地没有安装Oracle数据库的话 xff0c PLSQL是不能使用的 xff0c 输入远程数据库登录信息会提示 xff1a Oracle Client没有正确安装 这个
  • Ubuntu的中文乱码问题

    目标 xff1a 使系统 服务器支持中文 xff0c 能够正常显示 1 首先 xff0c 安装中文支持包language pack zh hans xff1a sudo apt get install language pack zh ha
  • Python argparse 处理命令行小结

    Python argparse 处理命令行小结 1 关于argparse 是python的一个命令行解析包 xff0c 主要用于处理命令行参数 2 基本用法 test py是测试文件 xff0c 其内容如下 import argparse
  • 分布式系统心跳协议的设计

    分布式系统心跳协议的设计 应用层心跳必不可少 xff1a 1 操作系统崩溃导致机器重启 没有机会发送 FIN 分节 2 服务器硬件故障导致机器重启 也没有机会发送 FIN 分节 3 并发连接数很高时 操作系统或进程如果重启 可能没有机会断开
  • malloc vs memset

    malloc vs memset OS内存分配过程如下 xff1a 用户态程序使用malloc接口 xff0c 分配虚拟地址 用户程序访问该虚拟地址 xff0c 比如memset 硬件 xff08 MMU xff09 需要将虚拟地址转换为物
  • c++ rvo vs std::move

    c 43 43 rvo vs std move To summarize RVO is a compiler optimization technique while std move is just an rvalue cast whic