C++ - 指向对象的向量的性能与对象的性能
In this case the question scenario is a game, so all resources are allocated at the beginning then iterated over for a level.
The objects being stored in the vector are instances of complex classes, and of course the actual copying them into the vector at load-time is time-consuming, but of low-concern.
But if my main concern is the speed of iteration over the class objects at runtime, would I be better to store the class objects themselves in the vector, rather than just pointers to the class objects as is traditionally recommended?
I am not worried about memory management in this example, only speed of iteration.
解决方案I'm answering this question late, but the performance aspect is important and the answers online so far have been purely theoretical and/or focusing exclusively on the memory-management aspects. So here is some actual benchmarking info on three related scenarios I recently tried. Your results may be different but at least there's some idea of how things pan out in a practical application.
The class A
referenced here has about 10 member fields, half of which are primitives and the other half are std::string
, std::vector<int>
, and other dynamically sized containers. The application has already been fairly optimized and thus we would like to see which architecture now gives us the fastest looping over the collection of A
. The values of any of A
object's member fields may be changing over the application lifetime, but the number of A
objects in the vector do not change over the many repeated iterations we perform (this continual iterating constitutes about 95% of this application's execution time). In all scenarios, looping was performed with the typical std::iterator
or std::const_iterator
. Each enumerated A
object has at least several member fields accessed.
Scenario 1 — Vector Of Object Pointers
Although the simplest, this architecture of std::vector<A*>
ended being slightly slower than the others.
Scenario 2 — Vector Of Object Pointers, Objects Are Allocated Using Placement New
The idea behind this approach is that we can improve the locality of caching by forcing our objects to be allocated into contiguous memory space. So the std::vector<A*>
of object pointers is guaranteed to be contiguous by the std::vector
implementation and the A
objects themselves will also be contiguous on the heap because we've used the placement new idiom. I used the same approach outlined in this answer; more info on placement new can be found here.
This scenario was 2.7% faster than Scenario 1.
Scenario 3 — Vector Of Objects
Here we use std::vector<A>
directly. The std::vector
implementation guarantees our A
objects will be contiguous in memory. Note that a std::vector
of objects does involve considerations of the move and copy constructors of A
. To avoid unnecessary moving and/or reconstruction, it is best to std::vector.reserve()
the maximum possibly needed size in advance (if possible) and then use std::vector.emplace_back()
(instead of push_back()
) if at all possible. Looping over this structure was the fastest because we are able to eliminate one level of pointer indirection.
This approach was 6.4% faster than Scenario 1.
A related answer to a different question also shows that plain objects (as class members) can be quite faster than the respective pointers (as class members).
相关文章