处理 std::stack 中的大量项目

发布于 2024-09-02 15:29:15 字数 63 浏览 2 评论 0原文

C++ std::stack 可以处理超过 10k int 项吗?而它的表现又如何呢?

Can C++ std::stack handle more than 10k int items? And how about its performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罪#恶を代价 2024-09-09 15:29:15

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.

不知所踪 2024-09-09 15:29:15

std::stack 仅受底层容器的限制(std::stack 是容器适配器)。

根据底层容器的不同,您将有不同的限制。

std::vector 将需要连续的内存,而 std::list 将仅受堆大小(以及可能的任何堆碎片)的限制。但 std::deque (默认容器)介于两者之间;它需要较小的连续内存块,但主要受到堆大小的限制。

A std::stack is only limited by the underlying container (a std::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 a std::list will only be limited by the size of the heap (and possibly any heap fragmentation). But std::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.

奈何桥上唱咆哮 2024-09-09 15:29:15

性能取决于所使用的底层容器。正如已经提到的,stack是一个适配器,底层容器可以是deque(默认),或vector,或list< /code> (全部位于 std 命名空间中)。

以下是性能比较的示例。由于问题中没有明确提到要存储的类型,我假设它是 unsigned int。请随意根据您的要求进行更改。该示例构建了一个包含 100K 项的堆栈。

stack.cpp内容

#include <stack>
#include <vector>
#include <list>
#include <deque>
#include <assert.h>

typedef unsigned int Type;

#ifdef USE_VECTOR
typedef std::stack< Type, std::vector< Type > > StackType;
#elif USE_LIST
typedef std::stack< Type, std::list< Type > > StackType;
#else
typedef std::stack< Type, std::deque< Type > > StackType;
#endif

int main() {
    StackType mystack;
    for( unsigned int i = 0; i < 100 * 1024; ++i ) {
        mystack.push( i );
    }
    assert( mystack.size() == 100 * 1024 );
    return 0;
}

执行对比:

$ g++ -DUSE_VECTOR stack.cpp -o stack; time ./stack

real    0m0.023s
user    0m0.030s
sys     0m0.031s

$ g++ -DUSE_LIST stack.cpp -o stack; time ./stack

real    0m0.144s
user    0m0.186s
sys     0m0.030s

$ g++  stack.cpp -o stack; time ./stack

real    0m0.024s
user    0m0.030s
sys     0m0.015s

asaha@nedata8 ~/code
$ 

以上数字为单次运行结果。 为了获得统计上显着的数字,请多次运行每个变体,并观察平均值和标准偏差。

显然,dequevector 会产生类似的结果性能,而 list 则较差。

The performance depends on the underlying container used. As already mentioned, stack is an adapter, the underlying container can be deque (the default), or vector, or list (all in std 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

#include <stack>
#include <vector>
#include <list>
#include <deque>
#include <assert.h>

typedef unsigned int Type;

#ifdef USE_VECTOR
typedef std::stack< Type, std::vector< Type > > StackType;
#elif USE_LIST
typedef std::stack< Type, std::list< Type > > StackType;
#else
typedef std::stack< Type, std::deque< Type > > StackType;
#endif

int main() {
    StackType mystack;
    for( unsigned int i = 0; i < 100 * 1024; ++i ) {
        mystack.push( i );
    }
    assert( mystack.size() == 100 * 1024 );
    return 0;
}

Execution comparison:

$ g++ -DUSE_VECTOR stack.cpp -o stack; time ./stack

real    0m0.023s
user    0m0.030s
sys     0m0.031s

$ g++ -DUSE_LIST stack.cpp -o stack; time ./stack

real    0m0.144s
user    0m0.186s
sys     0m0.030s

$ g++  stack.cpp -o stack; time ./stack

real    0m0.024s
user    0m0.030s
sys     0m0.015s

asaha@nedata8 ~/code
$ 

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 and vector results in similar performance, whereas list is worse.

掩饰不了的爱 2024-09-09 15:29:15

是的,它可以处理 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

悲念泪 2024-09-09 15:29:15

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文