3 ms·
It's not because of aliasing, it's because of the push_back call inside the inner loop, which may potentially have to allocate (in this case we know it doesn't,
by pbsd 13y ago
It's not because of aliasing, it's because of the push_back call inside the inner loop, which may potentially have to allocate (in this case we know it doesn't, but the compiler does not). If you replace squareVec6 with
vector<int> squareVec6(const vector<int>& v)
{
vector<int> result(v.size()); // preallocate
// result.reserve(v.size());
transform(begin(v), end(v), begin(result), [](int i)
{
return i*i;
});
return result;
}
the compiler will be happy to vectorize the loop.