使用std :: move是否对性能有好处? [英] Does the use of std::move have any performance benefits?

查看:320
本文介绍了使用std :: move是否对性能有好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

#include <iostream>
#include <vector>
#include <utility>

std::vector<int> vecTest;
int main() 
{
    int someRval = 3;
    vecTest.push_back(someRval);
    vecTest.push_back(std::move(someRval));
    return 0;
}

据我了解, someRval 的值将在第一次调用 push_back()时复制到vecTest中,但是在第二次调用someRval时会生成x值。我的问题是,会不会有任何性能上的好处,我的意思是可能不会使用 int ,但是在处理更大的对象时是否会有一些性能上的好处?

So as far as I understand, someRval's value will be copied into vecTest on the first call of push_back(), but on the second someRval produces an x value. My question is, will there ever be any performance benefit, I mean probably not with int but would there maybe be some performance benefit when working with much larger objects?

推荐答案

移动带来的性能优势通常来自动态分配被排除。

The performance benefit from moving usually comes from dynamic allocation being ruled out.

考虑一个过于简化(且天真)的字符串(缺少副本分配运算符和移动分配运算符):

Consider an over-simplified (and naive) string (missing a copy-assignment operator and a move-assignment operator):

class MyString
{
public:
    MyString() : data(nullptr) {}

    ~MyString()
    {
        delete[] data;
    }

    MyString(const MyString& other) //copy constructor
    { 
        data = new char[strlen(other.c_str()) + 1]; // another allocation
        strcpy(data, other.c_str()); // copy over the old string buffer
    }

    void set(const char* str)
    {
        char* newString = new char[strlen(str) + 1];
        strcpy(newString, str);
        delete[] data;
        data = newString;
    }

    const char* c_str() const
    {
        return data;
    }
private:
    char* data;
};

这一切都很好,但如果字符串变长,这里的复制构造函数可能会很昂贵。但是,必须使用copy构造函数来复制所有内容,因为不允许触摸 other 对象,它必须完全按照其名称进行操作, copy 内容。现在,如果您需要字符串的副本,这就是您必须支付的价格,但是如果您只想使用字符串的状态并且不关心之后会发生什么,您不妨移动

This is all fine and dandy but the copy constructor here is possibly expensive if your string becomes long. The copy constructor is however required to copy over everything because it's not allowed to touch the other object, it must do exactly what it's name says, copy contents. Now this is the price you have to pay if you need a copy of the string, but if you just want to use the string's state and don't care about what happens with it afterwards you might as well move it.

移动它只需要使 some 中的 other 对象保持有效状态,这样我们就可以使用 other 中的所有内容。现在,我们要做的只是复制我们的数据 data 指针所指向的内容。 c>指向 other 之一的指针,我们基本上是在窃取 other 的内容,很好,并将原始数据指针设置为 nullptr

Moving it only requires to leave the other object in some valid state so we can use everything in other which is exactly what we want. Now, all we have to do instead of copying the content our data pointer is pointing to is just to re-assign our data pointer to the one of other, we're basically stealing the contents of other, we'll also be nice and set the original data pointer to nullptr:

MyString(MyString&& other)
{
    data = other.data;
    other.data = nullptr;
}

这就是我们要做的。显然,这比像复制构造函数那样复制整个缓冲区快得多。

There, this is all we have to do. This is obviously way faster than copying the whole buffer over like the copy constructor is doing.

示例

这篇关于使用std :: move是否对性能有好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆