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

后端 未结 5 1082
轮回少年
轮回少年 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:41

    (Answer specific to C# as C++ may vary significantly.)

    1 and 2 are equivalent.

    3 would definitely be slower.

    Having said that, doing this a mere 300 times a second, you wouldn't notice any difference. Are you aware of just how much a computer can do in terms of raw CPU+memory in a second? In general, you should write code for clarity as the most important thing. By all means worry about performance - but only when you have a way to measure it, in order to a) tell whether you need to worry, and b) whether any changes actually improve the performance.

    In this case, I'd say that option 1 is the clearest, so that's what I'd use.

提交回复
热议问题