简体   繁体   English

优化向量分配c ++

[英]Optimizing vector assignment c++

I have a function which returns a vector. 我有一个返回向量的函数。 Let's name it v . 我们把它命名为v I use v.begin() and v.end() in a function. 我在函数中使用v.begin()v.end() How could I make using .begin() and .end() without creation a new vector v2 to store the previous vector v so I can get access to methods? 我怎样才能使用.begin().end()而无需创建一个新的向量v2来存储前一个向量v这样我就可以访问方法了?

Here's what I mean: 这就是我的意思:

std::vector<int> merge(...) {
    ...
    return v;
}
void mergeSort(...) {
    ...
    std::vector<int> v2;
    v2 = merge(...);
    I_need_this = v2.begin();
    And_I_really_need_this_too = v2.end();
    ...
}

The best way to optimize assignment is to not assign at all, but use initialization 优化分配的最佳方法是根本不分配,而是使用初始化

std::vector<int> v2 = merge(...);

It is almost certain that the compiler will create v directly into v2 , without any copying. 几乎可以肯定,编译器会直接在v2创建v ,而不进行任何复制。

A common pattern when you need to return collections, in order to prevent them from being copied, is to pass the collection by non-const reference and to make the changes to the original. 当您需要返回集合时,为了防止它们被复制,常见的模式是通过非const引用传递集合并对原始集合进行更改。 This means that the caller is responsible for instantiating the collection, not the callee, and as such no copy ever occurs. 这意味着调用者负责实例化集合,而不是被调用者,因此不会发生任何副本。 Here's an example: 这是一个例子:

void merge(std::vector<int> & result, ...) {

    // Do changes to result
    ...
}
void mergeSort(...) {
    ...
    std::vector<int> v;
    merge(v, ...);

    // This is OK now
    I_need_this = v.begin();
    And_I_really_need_this_too = v.end();
    ...
}

You can bind your vector temporary to a reference: 您可以将临时矢量绑定到引用:

extern std::vector<int> f();

auto&& x = f();
std::sort(x.begin(), x.end());  // example

You have a number of options: 你有很多选择:

  1. Don't worry about it. 别担心。 Performance is usually not that important. 性能通常不那么重要。

  2. Turn on optimization (you would be amazed how many "optimization" questions on SO are solved by that step). 启用优化(您会惊讶地发现SO上有多少“优化”问题通过该步骤解决)。

  3. Upgrade to a C++11 compiler. 升级到C ++ 11编译器。 The compiler should use the move assignment operator for vector, which is very efficient (any decent library will transfers the internally allocated array from the temporary to the target with no memory allocation and no copying of elements). 编译器应该使用向量的移动赋值运算符,这非常有效(任何像样的库都会将内部分配的数组从临时数据传输到目标而没有内存分配,也没有复制元素)。

  4. If you can't use a C++11 compiler, or you've measured, and this is still a performance bottleneck, switch to initialization as suggested by Bo Person in his answer 如果您不能使用C ++ 11编译器,或者您已经测量过,并且这仍然是性能瓶颈,请按照Bo Person在他的回答中的建议切换到初始化

  5. If you've measured, and this is still a performance bottleneck, and you can't get your compiler to implement the RVO, then you have no choice but to pass the vector in by value as suggest by André Fratelli in his answer 如果你已经测量过,这仍然是一个性能瓶颈,并且你无法让你的编译器实现RVO,那么你别无选择,只能按照AndréFratelli在他的回答中的建议传递矢量值

If we step back from discussing optimization, and discuss writing good, clear, code, then I would recommend in order of preference: 如果我们退出讨论优化,并讨论编写好的,清晰的代码,那么我会按优先顺序推荐:

  • Initialization from function result 从函数结果初始化
  • Assignment from function result 从功能结果分配
  • Pass in vector to be filled. 通过向量填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM