gpt4 book ai didi

c++ - std::queue 析构函数慢得要死,bug?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:08:29 24 4
gpt4 key购买 nike

<分区>

为什么 std::queue 中的析构函数非常慢?看看我的例子:

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!!

std::vector 中的析构函数非常快...

更新:我的编译器/IDE 是 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;
}

用 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.
}

更新 2:

现在一切都解决了!我卸载了 Visual Studio 2012 + Visual C++。我已经安装了 Visual Studio Community 2015,一切都快多了,并且按预期工作!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com