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

前端 未结 12 2032
悲哀的现实
悲哀的现实 2021-02-01 02:56

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:34

    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".

提交回复
热议问题