Incrementing: x++ vs x += 1

后端 未结 5 1919
囚心锁ツ
囚心锁ツ 2020-11-30 14:56

I\'ve read that many developers use x += 1 instead of x++ for clarity. I understand that x++ can be ambiguous for new developers and that x += 1 is always more clear, but i

5条回答
  •  囚心锁ツ
    2020-11-30 15:21

    When you say "it could add up in the long run" - don't think about it that way.

    Rather, think in terms of percentages. When you find the program counter is in that exact code 10% or more of the time, then worry about it. The reason is, if the percent is small, then the most you could conceivably save by improving it is also small.

    If the percent of time is less than 10%, you almost certainly have much bigger opportunities for speedup in other parts of the code, almost always in the form of function calls you could avoid.

    Here's an example.

提交回复
热议问题