我对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);
}
}
Run Code Online (Sandbox Code Playgroud)