What is faster (x < 0) or (x == -1)?

前端 未结 12 2081
栀梦
栀梦 2021-02-01 03:00

Variable x is int with possible values: -1, 0, 1, 2, 3. Which expression will be faster (in CPU ticks):

1. (x < 0)
2. (x == -1)
         


        
12条回答
  •  不知归路
    2021-02-01 03:25

    Try it and see! Do a million, or better, a billion of each and time them. I bet there is no statistical significance in your results, but who knows -- maybe on your platform and compiler, you might find a result.

    This is a great experiment to convince yourself that premature optimization is probably not worth your time--and may well be "the root of all evil--at least in programming".

提交回复
热议问题