相关疑难解决方法(0)

std :: queue <T,list <T >>> :: size()在O(n)中是否很慢?

我遇到了使用队列的代码的意外性能行为.我意识到当队列中有更多元素时,性能会下降.事实证明,使用该size()方法是原因.以下是一些显示问题的代码:

#include <queue>
#include <list>
#include <iostream>

#include "Stopwatch.h"

using namespace std;

struct BigStruct
{
    int x[100];
};
int main()
{
    CStopwatch queueTestSw;

    typedef BigStruct QueueElementType;
    typedef std::queue<QueueElementType, std::list<QueueElementType> > QueueType;
    //typedef std::queue<QueueElementType > QueueType; //no surprise, this queue is fast and constant
    QueueType m_queue;

    for (int i=0;i<22000;i++)
        m_queue.push(QueueElementType());
    CStopwatch sw;
    sw.Start();
    int dummy;
    while (!m_queue.empty())
    {
        //remove 1000 elements:
        for (int i=0;i<1000;i++)
        {
            m_queue.pop();
        }
        //call size() 1000 times and see how long it takes
        sw = …
Run Code Online (Sandbox Code Playgroud)

c++ queue performance stl

5
推荐指数
2
解决办法
2786
查看次数

标签 统计

c++ ×1

performance ×1

queue ×1

stl ×1