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

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

    I'm sure you're confident this is a real time-taker.

    I would suppose asking the machine would give a more reliable answer than any of us could give.

    I've found, even in code like you're talking about, my supposition that I knew where the time was going was not quite correct. For example, if this is in an inner loop, if there is any sort of function call, even an invisible one inserted by the compiler, the cost of that call will dominate by far.

提交回复
热议问题