简体   繁体   English

可变大小的char数组,同时最大限度地减少了对new的调用?

[英]Variable sized char array with minimizing calls to new?

I need a char array that will dynamically change in size. 我需要一个可以动态改变大小的char数组。 I do not know how big it can get so preallocating is not an option. 我不知道它有多大,所以预分配不是一个选择。 It could never get any bigger than 20 bytes 1 time, the next time it may get up to 5kb... 1次它永远不会超过20个字节,下一次它可能高达5kb ...

I want the allocation to be like a std vector. 我希望分配像一个标准向量。

I thought of using a std vector < char > but all those push backs seem like they waste time: 我想到使用std向量<char>,但是所有这些后退似乎都在浪费时间:

strVec.clear();
for(size_t i = 0; i < varLen; ++i)
{
   strVec.push_back(0);
}

Is this the best I can do or is there a way to add a bunch of items to a vector at once? 这是我能做的最好的吗,还是有一种方法可以一次向向量添加一堆项目? Or maybe a better way to do this. 或者,也许是一种更好的方法。

Thanks 谢谢

std::vector不会在每次调用push_back时分配内存,仅在大小变得大于容量时才分配

First, don't optimize until you've profiled your code and determined that there is a bottleneck. 首先,在分析了代码并确定存在瓶颈之前,请不要进行优化。 Consider the costs to readability, accessibility, and maintainability by doing something clever. 通过做一些聪明的事来考虑可读性,可访问性和可维护性的成本。 Make sure any plan you take won't preclude you from working with Unicode in future. 确保您采取的任何计划都不会阻止您将来使用Unicode。 Still here? 还在? Alright. 好的。

As others have mentioned, vectors reserve more memory than they use initially, and push_back usually is very cheap. 正如其他人提到的那样,向量保留的内存比起初使用的要多,而push_back通常非常便宜。

There are cases when using push_back reallocates memory more than is necessary, however. 但是,在某些情况下,使用push_back会重新分配所需的内存。 For example, one million calls to myvector.push_back() might trigger 10 or 20 reallocations of myvector. 例如,对myvector.push_back()的一百万次调用可能会触发myvector的10或20个重新分配。 On the other hand, inserting into a vector at its end will cause at most 1 reallocation of myvector*. 另一方面,在向量的末端插入向量将导致myvector *最多重新分配1次。 I generally prefer the insertion idiom to the reserve / push_back idiom for both speed and readability reasons. 由于速度和可读性的原因,我通常更喜欢插入习惯而不是reserve / push_back习惯。

myvector.insert(myvector.end(), inputBegin, inputEnd)

If you do not know the size of your string in advance and cannot tolerate the hiccups caused by reallocations, perhaps because of hard real-time constraints, then maybe you should use a linked list. 如果您事先不知道字符串的大小并且不能忍受由重新分配(可能是由于严格的实时约束)而导致的打ic,那么也许应该使用链接列表。 A linked list will have consistent performance at the price of much worse average performance. 链表将具有稳定的性能,但平均性能却要差得多。

If all of this isn't enough for your purposes, consider other data structures such as a rope or post back with more specifics about your case. 如果这还不足以满足您的目的,请考虑使用其他数据结构,例如绳索或回寄有关案件的更多详细信息。

  • From Scott Meyer's Effective STL, IIRC 摘自Scott Meyer的有效STL,IIRC

You can use the resize member function to add a bunch. 您可以使用resize成员函数添加一堆。 However, I would not expect that push_back would be slow, especially if the vector's internal capacity is already non-trivial. 但是,我不希望push_back会变慢,特别是如果向量的内部容量已经很重要的话。

Is this the best I can do or is there a way to add a bunch of items to a vector at once? 这是我能做的最好的吗,还是有一种方法可以一次向向量添加一堆项目? Or maybe a better way to do this. 或者,也许是一种更好的方法。

push_back isn't very slow, it just compares the size to the current capacity and reallocates if necessary. push_back不是很慢,它只是将大小与当前容量进行比较,并在必要时重新分配。 The comparison may work out to essentially zero time because of branch prediction and superscalar execution on the CPU. 由于在CPU上有分支预测和超标量执行,因此比较可能会基本达到零时间。 The reallocation is performed O(log N) times, so the vector uses up to twice as much memory as needed but time spent on reallocation seldom adds up to anything. 重新分配执行O(log N)次,因此向量最多使用所需内存的两倍,但是花费在重新分配上的时间很少累加任何东西。

To insert several items at once, use insert . 要一次插入多个项目,请使用insert There are a few overloads, the only trick is that you need to explicitly pass end . 有一些重载,唯一的技巧是您需要显式传递end

my_vec.insert( my_vec.end(), num_to_add, initial_value );
my_vec.insert( my_vec.end(), first, last ); // iterators or pointers

For the second form, you could put the values in an array first and then copy the array to the end of the vector. 对于第二种形式,可以先将值放在数组中,然后将数组复制到向量的末尾。 But this might add as much complexity as it removes. 但这可能会增加所消除的复杂性。 That's how it goes with micro-optimization. 这就是微优化的过程。 Only attempt to optimize if you know there's a measurable gain to be had. 仅当您知道有可衡量的收益时,才尝试进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM