Is < faster than <=?

后端 未结 14 854
孤城傲影
孤城傲影 2020-11-22 13:43

Is if( a < 901 ) faster than if( a <= 900 ).

Not exactly as in this simple example, but there are slight performance changes on loop

14条回答
  •  忘了有多久
    2020-11-22 14:13

    I see that neither is faster. The compiler generates the same machine code in each condition with a different value.

    if(a < 901)
    cmpl  $900, -4(%rbp)
    jg .L2
    
    if(a <=901)
    cmpl  $901, -4(%rbp)
    jg .L3
    

    My example if is from GCC on x86_64 platform on Linux.

    Compiler writers are pretty smart people, and they think of these things and many others most of us take for granted.

    I noticed that if it is not a constant, then the same machine code is generated in either case.

    int b;
    if(a < b)
    cmpl  -4(%rbp), %eax
    jge   .L2
    
    if(a <=b)
    cmpl  -4(%rbp), %eax
    jg .L3
    

提交回复
热议问题