for std::queue remove all of element swap 和 pop 的时间差

Time difference between swap and pop for for std::queue remove all of element

我正在尝试清除 std::queue 以便已经有一些关于此的冒充。 How do I clear the std::queue efficiently?

我试图为此编写一个简单的代码。我对 "swap" 和 "pop" 方法之间的时差有疑问。

测试1和测试2的总时间相同。但是,方法内部结果不同。

案例测试 1:队列弹出

      Wed Jul 22 11:33:33 2015 : 10000000 start queue
      Wed Jul 22 11:33:38 2015 : 10000000 queue push complete
       diff : 5592 milliseconds(msec)
      Wed Jul 22 11:33:38 2015 : 10000000 clear queue
      Wed Jul 22 11:33:42 2015 : 10000000 queue clear complete
       diff : 3561 milliseconds(msec)
       diff between after TEST1 : 135644 milliseconds(msec)

案例测试 2:队列交换

       Wed Jul 22 11:37:45 2015 : 10000000 start queue
      Wed Jul 22 11:37:51 2015 : 10000000 queue push complete
       diff : 5875 milliseconds(msec)
      Wed Jul 22 11:37:51 2015 : 10000000 clear queue
      Wed Jul 22 11:40:00 2015 : 10000000 queue clear complete
       diff : 129130 milliseconds(msec)
       diff between after TEST2 : 135006 milliseconds(msec)

有什么它起作用的原因吗?

环境: Windows7 (x64), MSVC2013

代码片段:

#include <iostream>
#include <exception>
#include <chrono>
#include <queue>
#include <vector>
#include <ctime>
#include <string>

using namespace std;

std::string asString(const std::chrono::system_clock::time_point& tp)
{
    std::time_t t = std::chrono::system_clock::to_time_t(tp);
    std::string ts = std::ctime(&t);
    ts.resize(ts.size() - 1);
    return ts;
}

template<class T>
void clear(std::queue<T> &q)
{
    std::queue<T> empty;
    std::swap(q, empty);
}

void Test1(int itemCount, int mode)
{
    queue<int> q;
    std::chrono::system_clock::time_point tp_push = std::chrono::system_clock::now();
    std::cout << asString(tp_push) << " : " << itemCount << " start queue : mode - " << mode << endl;

    for (int i = 0; i < itemCount; i++)
    {
        q.push(i);
    }

    std::cout << asString(std::chrono::system_clock::now()) << " : " << itemCount << " queue push complete " << endl;
    auto diff_push = std::chrono::system_clock::now() - tp_push;
    std::cout << " diff : "
        << chrono::duration_cast<chrono::milliseconds>(diff_push).count()
        << " milliseconds(msec) " << endl;

    std::chrono::system_clock::time_point tp_clear = std::chrono::system_clock::now();
    std::cout << asString(tp_clear) << " : " << itemCount << " clear queue " << endl;

    if (mode)
    {
        clear(q);
    }
    else
    {
        while (!q.empty())
        {
        q.pop();
        }
    }

    std::cout << asString(std::chrono::system_clock::now()) << " : " << itemCount << " queue clear complete " << endl;
    auto diff_clear = std::chrono::system_clock::now() - tp_clear;
    std::cout   << " diff : "
                    << chrono::duration_cast<chrono::milliseconds>(diff_clear).count()
                    << " milliseconds(msec) " << endl;
}

int main()
{
    try 
    {
        int itemCount = 10000000;

        std::chrono::system_clock::time_point tp_test1 = std::chrono::system_clock::now();

        Test1(itemCount, 0);  

        auto diff_test1 = std::chrono::system_clock::now() - tp_test1;
        std::cout << " diff between after TEST1 : "
            << chrono::duration_cast<chrono::milliseconds>(diff_test1).count()
            << " milliseconds(msec) " << endl;

        std::chrono::system_clock::time_point tp_test2 = std::chrono::system_clock::now();

        Test1(itemCount, 1);

        auto diff_test2 = std::chrono::system_clock::now() - tp_test2;
        std::cout << " diff after TEST2 : "
            << chrono::duration_cast<chrono::milliseconds>(diff_test2).count()
            << " milliseconds(msec) " << endl;


    }
    catch (const exception& e)
    {
        cerr << "EXCEPTION : " << e.what() << endl;
    }
}

看起来 std::queue 交换时相当于 while()/pop()。如果底层结构是链表,则尤其如此,因为必须对每个元素进行迭代以释放内存。

交换似乎与 while/pop 循环做同样事情的原因是因为您已经 'moved' 将空队列中的数据放入您的队列对象(反之亦然)- 您的队列对象现在是空的,但是您所做的只是将数据移动到一个临时队列对象中,一旦清除函数 returns,该对象就会立即被释放,这是由于 empty 的自动作用域导致其析构函数打电话。