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

前端 未结 12 2051
悲哀的现实
悲哀的现实 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:33

    The important consideration, anyway, is which actually directs your program flow accurately, and which just happens to produce the same result?

    If x is actually and index or a value in an enum, then will -1 always be what you want, or will any negative value work? Right now, -1 is the only negative, but that could change.

提交回复
热议问题