It is entirely up to you, your code, and your compiler. Imagine that you have:
Now, if your compiler is optimized for size, it can leave it as it is. But if it optimizes for performance, it may (or maybe not, some compilers use crude heuristic measures to determine this), convert this value to:
int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; ++i) { results[i] = lhs[i] + rhs[i]; } }
If he optimizes even more, he can unroll
int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; i+=4) { results[i] = lhs[i] + rhs[i]; results[i+1] = lhs[i+1] + rhs[i+1]; results[i+2] = lhs[i+2] + rhs[i+2]; results[i+3] = lhs[i+3] + rhs[i+3]; } }
Size increased. But if the compiler now also solves a bit of automatic vectorization, it can transform again into something not like this:
int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; i+=4) { vec4_add (&results[i], &lhs[i], &rhs[i]); } }
Size reduced.
Next, the compiler, as always, smart, deploys again and completely kills the loop:
int main () { std::vector<int> results(20), lhs(20), rhs(20); vec4_add (&results[i], &lhs[i], &rhs[i]); vec4_add (&results[i+4], &lhs[i+4], &rhs[i+4]); vec4_add (&results[i+8], &lhs[i+8], &rhs[i+8]); vec4_add (&results[i+12], &lhs[i+12], &rhs[i+12]); vec4_add (&results[i+16], &lhs[i+16], &rhs[i+16]); }
G ++ optimization will be performed if it can be done enough to replace a vector with a regular array
int main () { int results[20] = {0}, lhs[20] = {0}, rhs[20] = {0}; vec4_add (&results[i], &lhs[i], &rhs[i]); vec4_add (&results[i+4], &lhs[i+4], &rhs[i+4]); vec4_add (&results[i+8], &lhs[i+8], &rhs[i+8]); vec4_add (&results[i+12], &lhs[i+12], &rhs[i+12]); vec4_add (&results[i+16], &lhs[i+16], &rhs[i+16]); }
He sees how everything is constant, and develops
int main () { int results[20] = {0};
He concludes that the results are not actually used and finally spit out:
int main() { }