简体   繁体   English

std :: queue的swap和pop之间的时差删除所有元素

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

I'm trying to clear std::queue so that there are already some posing about this. 我正在尝试清除std::queue以便已经对此进行了一些摆弄。 How do I clear the std::queue efficiently? 如何有效清除std :: queue?

I've tried to make a simple code for this. 我试图为此编写一个简单的代码。 I have a question for time difference between "swap" and just "pop" method. 我有一个关于“交换”和“流行”方法之间的时差的问题。

Test1 and Test2 are same total time. Test1和Test2是相同的总时间。 However, inside of method result is different. 但是,方法内部的结果不同。

CASE Test1: queue pop 案例测试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) 

CASE Test2: queue swap 案例Test2:队列交换

  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) 

Is there any reason why it works? 有什么理由起作用吗?

Environment: Windows7 (x64), MSVC2013 环境:Windows7(x64),MSVC2013

Code snippet: 程式码片段:

#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;
    }
}

It looks like std::queue when swapped does the equivalent of while()/pop(). 看起来像std::queue时,被替换为while()/ pop()。 This is especially true if the underlying structure is a linked list, as it would have to be iterated over each element to free up the memory. 如果基础结构是一个链表,则尤其如此,因为必须在每个元素上进行迭代以释放内存。

The reason swap appears to do the same thing as the while/pop loop is because you've 'moved' the data from an empty queue into your queue object (and vice versa) - your queue object is now empty, but all you've done is move the data into a temporary queue object that is immediately freed once the Clear function returns, due to the automatic scoping of empty resulting in its descructor being called. 交换似乎与while / pop循环发生相同的原因是因为您已将数据从空队列“移”到队列对象中(反之亦然)-队列对象现在为空,但是您所要做的只是完成后,将数据移到临时队列对象中,一旦Clear函数返回,该对象将立即释放,这是由于对empty的自动作用域导致调用了其描述符。

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

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