0

I was wondering if this code

for (int i=0; i <= n; i++)
{
    someArray[i] = i;
}

will be slower than initializing array line by line like this

someArray[0] = 0;
someArray[1] = 1;
.
.
.
someArray[n] = n;

It seems like if there is no compiler optimization for this, the for loop would be slower since it need to create a new variable i, check the conditional statement and increment i. I was wondering if there is actually a compiler time optimization that optimizes scenarios like this.

4

1 回答 1

0

编译器可以进行循环展开以使其更快。

于 2017-05-24T03:06:28.450 回答