C++ - Performance of vector of pointer to objects, vs performance of objects(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).
这篇关于C++ - 指向对象的向量的性能与对象的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ - 指向对象的向量的性能与对象的性能
基础教程推荐
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- C++定义类对象 1970-01-01
- C语言访问数组元素 1970-01-01
- C++ #define 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 初始化变量和赋值运算符 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- C++按值调用 1970-01-01