简体   繁体   English

是否应该使用std :: vector +我自己的大小变量?

[英]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.

相关问题 我应该在我的代码中使用std :: vector :: at() - Should I use std::vector::at() in my code 我应该使用std :: unique_ptr <T> 在我班级的std :: vector成员变量中? - Should I use std::unique_ptr<T> in a std::vector member variable in my class? 我应该总是使用带有自己索引的一维向量,还是多维向量? - Should I always use a 1D vector with my own indexing, or is a multi-dimensional vector ok? 当数字明确给出时,我应该参考std :: vector的大小吗 - Should I refer std::vector size when the number is explicitly given 为什么/何时应该在std :: vector &lt;&gt;上使用std :: unique / shared_ptr(std :: vector &lt;&gt;)? - Why/when should I use std::unique/shared_ptr (std::vector<>) over just std::vector<>? 什么时候应该使用自己的名称空间? - When I should use my own namespaces? 如何将 std::condition_variable 与我自己的互斥体包装器一起使用 - How to use std::condition_variable with my own mutex wrapper 我应该使用std :: size_t来计算类的实例吗? - Should I use std::size_t to count instance of my class? 我想将我自己的随机 function 与 std::shuffle 一起使用,但它不起作用 - I want to use my own random function with std::shuffle, but it is not working 我的函数应该返回一个指向std :: vector的指针,还是对std :: vector的引用? - Should my function return a pointer to std::vector, or a reference to std::vector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM