处理 std::stack 中的大量项目
C++ std::stack
可以处理超过 10k int 项吗?而它的表现又如何呢?
Can C++ std::stack
handle more than 10k int items? And how about its performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
10K什么?大多数 std::stack 实现可以轻松处理 10K 基元并具有良好的性能,因为这可能小于 10K。 80KB。但是,该标准并不保证性能。
另请注意,这取决于您使用的后端。默认值是 std::deque,它基本上是一个数组的数组。但是,任何后端都应该获得不错的性能。
10K what? Most std::stack implementations could easily handle 10K primitives with good performance, since that's probably < 80KB. However, the standard doesn't guarantee performance.
Also note that it depends what backend you use. The default is std::deque, which is basically an array of arrays. However, you should get decent performance with any backend.
std::stack
仅受底层容器的限制(std::stack
是容器适配器)。根据底层容器的不同,您将有不同的限制。
std::vector
将需要连续的内存,而std::list
将仅受堆大小(以及可能的任何堆碎片)的限制。但 std::deque (默认容器)介于两者之间;它需要较小的连续内存块,但主要受到堆大小的限制。A
std::stack
is only limited by the underlying container (astd::stack
is a container adapter).Depending on what the underlying container is, you'll have different limits.
A
std::vector
will require contiguous memory whereas astd::list
will only be limited by the size of the heap (and possibly any heap fragmentation). Butstd::deque
(the default container) is between the two; it requires smaller chunks of contiguous memory but will primarily be limited by the size of the heap.性能取决于所使用的底层容器。正如已经提到的,
stack
是一个适配器,底层容器可以是deque
(默认),或vector
,或list< /code> (全部位于
std
命名空间中)。以下是性能比较的示例。由于问题中没有明确提到要存储的类型,我假设它是 unsigned int。请随意根据您的要求进行更改。该示例构建了一个包含 100K 项的堆栈。
stack.cpp
内容执行对比:
以上数字为单次运行结果。 为了获得统计上显着的数字,请多次运行每个变体,并观察平均值和标准偏差。
显然,
deque
和vector
会产生类似的结果性能,而list
则较差。The performance depends on the underlying container used. As already mentioned,
stack
is an adapter, the underlying container can bedeque
(the default), orvector
, orlist
(all instd
namespace).Following is an example of performance comparison. As the type to be stored is not clearly mentioned in the question, I am assuming it to be unsigned int. Feel free to change it per your requirements. The example constructs a stack of 100K items.
Contents of
stack.cpp
Execution comparison:
The above numbers are result of single run. To achieve statistically significant numbers run each variation large number of times, and observe the mean and standard deviation.
Apparently,
deque
andvector
results in similar performance, whereaslist
is worse.是的,它可以处理 10k。只要你有足够的内存资源。如果您担心它使用内部堆栈,那么它不会。
性能取决于具体实现,但应该非常快
Yes, it can handle 10k. As long as you have the memory resources for it. If you are worried it uses the internal stack, it doesn't.
Performance is implementation specific, but should be very quick
std::stack 不是容器,而是容器适配器。默认情况下,它使用 std::deque 作为容器,但您也可以使用 std::vector 或 std::list。当元素被删除时,双端队列释放它们的内存。
std::stack is not a container, but a container adapter. By default it uses an std::deque as container, but you can also use a std::vector or std::list. Deques free their memory when elements are removed.