<分区>
为什么 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,一切都快多了,并且按预期工作!