Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

27
  • 232
    I'm willing to bet that a modern compiler will generate the same assembly for both of those cases. Commented Sep 4, 2018 at 18:25
  • 13
    I rollbacked the question to the original state, since your edit invalidated my answer - please don't do that! If you ask a question how to improve your code, then don't change the question by improving the code in the shown way - this makes the answers look meaningless. Commented Sep 4, 2018 at 19:40
  • 77
    Wait a second, they asked to get rid of int s while being totally fine with those magic numbers for upper and lower bounds? Commented Sep 4, 2018 at 20:52
  • 34
    Remember: profile before optimizing. With modern compilers, Method A and Method B may be optimized to the same code (using higher optimization levels). Also, with modern processors, they could have instructions that perform more than addition in a single operation. Commented Sep 4, 2018 at 22:07
  • 142
    Neither; optimize for readability. Commented Sep 4, 2018 at 23:19