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

前端 未结 12 2078
栀梦
栀梦 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:17

    Nikolay, you write:

    It's actually bottleneck operator in the high-load program. Performance in this 1-2 strings is much more valuable than readability...

    All bottlenecks are usually this small, even in perfect design with perfect algorithms (though there is no such). I do high-load DNA processing and know my field and my algorithms quite well

    If so, why not to do next:

    1. get timer, set it to 0;
    2. compile your high-load program with (x < 0);
    3. start your program and timer;
    4. on program end look at the timer and remember result1.
    5. same as 1;
    6. compile your high-load program with (x == -1);
    7. same as 3;
    8. on program end look at the timer and remember result2.
    9. compare result1 and result2.

    You'll get the Answer.

提交回复
热议问题