Std :: queue destructor is a hell slow error?

Why is the destructor std::queuevery slow? look at my example:

void test()
{
    int total = 17173512;
    std::queue<int> q;
    for(int i = 0; i < total; i++)
        q.push(i); //Push is very fast.

    std::cout<<"Done"<<std::endl; //Less than a second.
}

test();
std::cout<<"Done"<<std::endl; //This takes several minutes!!

Destructor in a std::vectorvery fast ...

UPDATED: My compiler / IDE is Visual Studio 2012 Visual C ++.

int main(int argc, char **argv)
{
    int total = 17173512;
    std::queue<int> q;
    for(int i = 0; i < total; i++)
        q.push(i);

    std::cout<<"Done0"<<std::endl; //This takes less than a second.

    while(!q.empty())
        q.pop();

 //This takes less than a second. Memory should be deallocated here!
    std::cout<<"Done1"<<std::endl;

    //Waiting forever, i.e. deallocating(HERE??) memory EXTREMELY SLOWLY.
   //I can see how the memory is being deallocated here in windows task manager!
        return 0;
}

With vector:

int main(int argc, char **argv)
{
    int total = 17173512;
    std::vector<int> q(total);
    for(int i = 0; i < total; i++)
        q[i] = 2000;

    std::cout<<"Done"<<std::endl;

    return 0; //Extremely fast.
}

UPDATE 2:

Everything is decided now! I uninstalled Visual studio 2012 + visual C ++. I installed Visual Studio Community 2015 and everything is much faster and works as expected!

+4
source share
3 answers

I used this code to measure:

int main(){
    auto test = [](){
        auto start = std::chrono::system_clock::now();
        int total = 17173512;
        std::queue<int> q;
        for (int i = 0; i < total; i++){
            q.push(i); //Push is very fast.
        }
        auto end = std::chrono::system_clock::now();
        return end - start;
    };

    auto start = std::chrono::system_clock::now();

    auto pushing_time = test();

    auto end = std::chrono::system_clock::now();
    auto deleting_time = (end - start) - pushing_time;

    std::cout << "Pushing Time:" << pushing_time.count() << '\n';
    std::cout << "Deleting Time:" << deleting_time.count() << '\n';
    return 0;
}

Environment:

  • Intel i7-4510U @ 2.00 GHz
  • Windows 8.1
  • MSVS 2013

Results:

, \O2, VS:

: 71403190

: 5293067027

, \O2, VS:

: 3743267

: 1741230

, , , IDE.

+2

n , n , meory 1 ( ).

, , n , n (, ). .

0

As a rule, Christoph is right about the type of container, but here the problem does not depend on the type of container. we expect memory allocation and release to be nearly equal. I analyzed the code in MSVC2010 in debug and release mode. Analysis showed that the delete operation takes longer than the new operator. it seems that the delete operation includes some additional operations, but I don’t know for sure.

0
source

All Articles