[英]Should I use std::vector + my own size variable or not?
Note: Performance is very critical in my application! 注意:在我的应用程序中,性能至关重要! Allocate enough buffer storage for the worst case scenario is a requirement to avoid reallocation. 为避免重新分配,有必要为最坏的情况分配足够的缓冲区存储。
Look at this, this is how I usually use std::vector: 看看这个,这就是我通常使用std :: vector的方式:
//On startup...
unsigned int currVectorSize = 0u;
std::vector<MyStruct> myStructs;
myStructs.resize(...); //Allocate for the worst case scenario!
//Each frame, do this.
currVectorSize = 0u; //Reset vector, very fast.
run algorithm...
//insert X elements in myStructs if condition is met
myStructs[currVectorSize].member0 = ;
myStructs[currVectorSize].member1 = ;
myStructs[currVectorSize].member2 = ;
currVectorSize++;
run another algorithm...
//insert X elements in myStructs if condition is met
myStructs[currVectorSize].member0 = ;
myStructs[currVectorSize].member1 = ;
myStructs[currVectorSize].member2 = ;
currVectorSize++;
Another part of the application uses myStructs and currVectorSize
I have a decision problem, should I use std::vector + resize + my own size variable OR std::vector + reserve + push_back + clear + size? 我有一个决策问题,应该使用std :: vector +调整大小+我自己的大小变量还是std :: vector +保留+ push_back +清除+大小?
I don't like to keep another size variable floating around, but the clear() function is slow(linear time) and the push_back function have the overhead of bounds check. 我不喜欢让另一个size变量浮动,但是clear()函数的速度很慢(线性时间),而push_back函数具有边界检查的开销。 I need to reset the size variable in constant time each frame without calling any destructors and running in linear time. 我需要在每个帧的固定时间内重置size变量,而无需调用任何析构函数并在线性时间内运行。 Conclusion: I don't want to destroy my old data, I just need to reset the current size/current number inserted elements variable each frame. 结论:我不想破坏我的旧数据,我只需要重置每帧插入的当前大小/当前数量的元素变量即可。
If performance is critical, then perhaps you should just profile everything you can. 如果性能至关重要,那么也许您应该概要介绍所有可能的信息。
Using your own size variable can help if you can be sure that no reallocation is needed beforehand (this is what you do - incrementing currVectorSize
with no checks), but in this case why use std::vector
at all? 如果可以确定事先不需要重新分配,则使用自己的size变量会有所帮助(这是您要做的-在不检查的情况下递增currVectorSize
),但是在这种情况下,为什么要完全使用std::vector
? Just use an array or std::array
. 只需使用array或std::array
。
Otherwise (if reallocation could happen) you would still need to compare your size variable to actual vector size, so this will be pretty much the same thing push_back
does and will gain you nothing. 否则(如果可能发生重新分配),您仍然需要将您的大小变量与实际向量大小进行比较,因此,这与push_back
所做的几乎一样,并且不会给您带来任何好处。
There are also some tweaked/optimized implementations of vector
like folly::fbvector
but you should carefully consider (and again, profile) wheter or not you need something like that. 还有一些对vector
进行了调整/优化的实现,例如folly::fbvector
但是您应该仔细考虑(再分析一次),是否需要这样的东西。
As for clearing the vector, check out vector::resize
- it is actually guaranteed not to reallocate if you're resizing down (due to iterator invalidation). 至于清除向量,请检出vector::resize
实际上,如果要缩小vector::resize
,则保证不会重新分配(由于迭代器无效)。 So you can call resize(0)
instead of clear
just to be sure. 因此,可以确定,您可以调用resize(0)
而不是clear
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.