我有一个关于在C++中std :: vector <>的性能的问题。通过调用clear()方法重用相同的矢量还是快速重新创建矢量更快?更快:重新创建或清除()?
下面的例子是没有现实生活中的代码,它只是为了明确的问题是什么:
//Example ONE: is this faster
std::vector<int> foo;
for(int i = 0; i < 100; ++i)
{
foo.clear();
for(int j = 0; j < 100; ++j)
{
foo.push_back(i+j);
}
}
//Example TWO: or is that faster?
for(int i = 0; i < 100; ++i)
{
std::vector<int> foo;
for(int j = 0; j < 100; ++j)
{
foo.push_back(i+j);
}
}
这些东西不是依赖于实现吗? –
配置文件。 Boooooring! –
为什么所有的downvotes? – Daniel