[Solved] C++ vector v, vector*v, vector vec, which is the fastest (or the most efficient)? [closed]


There is a big misunderstanding here: You are comparing vectors with different semantics. It’s like asking which is faster, a glass of water or a banana.

If you think about using a vector of pointers, std::vector<T*>, you need to be aware that you are using manual memory management. It might be faster in some use-cases, but it’s also way more error prone. If your use-case is appropriate, you might consider std::vector<std::shared_ptr<T>> or std::vector<std::unique_ptr<T>> instead.

std::vector<T>* is something else entirely. You need to learn about the background of how, when and why to use plain pointers (hint: they are rarely used at all these days), smart pointers or simply no pointers and instead use the objects directly.

In terms of efficiency, even a normal std::vector<T> can nowadays be quite efficient due to move semantics. Google it and learn about it as well. Also, efficiency does not just come from the number of memory allocations, but also from the memory layout. This is where a std::vector<T> can often provide severe benefits over the alternatives.

And with everything which is about efficiency: Measure it! Without measuring, you will never really know what is better for your use-case. Start with the most obvious and correct code (that is to say: Avoid manual memory management), measure it, find the bottlenecks, only then remove them by possibly adding more complexity.

1

solved C++ vector v, vector*v, vector vec, which is the fastest (or the most efficient)? [closed]