x=x+1 vs. x +=1

后端 未结 17 1228
独厮守ぢ
独厮守ぢ 2020-12-29 02:57

I\'m under the impression that these two commands result in the same end, namely incrementing X by 1 but that the latter is probably more efficient.

If this is not c

17条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 03:27

    On x86, if x is in register eax, they will both result in something like

    inc eax;

    So you're right, after some compilation stage, the IL will be the same.

    There's a whole class of questions like this that can be answered with "trust your optimizer."

    The famous myth is that
    x++;
    is less efficient than
    ++x;
    because it has to store a temporary value. If you never use the temporary value, the optimizer will remove that store.

提交回复
热议问题