Which one of the two ways of number comparison is more efficient [closed]

I see no point if such comparisons, but if you are really curious, let’s compare assembly generated by VC11:

int getmax_1(int a,int b)
{
    if (a>b)
        return a;
    else
        return b;
}

int getmax_2(int a,int b)
{
    if (a>b)
        return a;

    return b;
}

getmax_1

    if (a>b)
002017BE  mov         eax,dword ptr [a]  
002017C1  cmp         eax,dword ptr [b]  
002017C4  jle         getmax_1+2Dh (02017CDh)  
    return a;
002017C6  mov         eax,dword ptr [a]  
002017C9  jmp         getmax_1+30h (02017D0h)  
    else
002017CB  jmp         getmax_1+30h (02017D0h)  
    return b;
002017CD  mov         eax,dword ptr [b]

getmax_2

    if (a>b)
002017FE  mov         eax,dword ptr [a]  
00201801  cmp         eax,dword ptr [b]  
00201804  jle         getmax_2+2Bh (020180Bh)  
    return a;
00201806  mov         eax,dword ptr [a]  
00201809  jmp         getmax_2+2Eh (020180Eh)  
    return b;
0020180B  mov         eax,dword ptr [b]  

This is, however, a Debug build. In release build, these two calls will be definitely inlined and additional call in second function probably eliminated.

So… no difference, really.

Leave a Comment