Which is faster? ++, += or x + 1?

后端 未结 5 1073
轮回少年
轮回少年 2020-12-10 01:41

I am using C# (This question is also valid for similar languages like C++) and I am trying to figure out the fastest and most efficient way to increment. It isn\'t just one

5条回答
  •  执笔经年
    2020-12-10 02:39

    Options 1 and 2 will result in identical code being produced by the compiler. Option 3 will be much slower.

    It's a fallacy that i++ is faster than i += 1 or even i = i + 1. All decent compilers will turn those three instructions into the same code.

    For such a trivial operation as addition, write the clearest code and let the compiler worry about making it fast.

提交回复
热议问题