c++ - Which one of the two ways of number comparison is more efficient -


in function number comparison, can use if clauses, 1 use if else, other directly use return. since if not return big one, other bigger one.

now question is: more efficient? tested timing function, fails differentiate.

method a:

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

method b:

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

sorry making of unhappy. have question because want know else clause contribute timing. else clause cost more time? although run 100000000 times in loop, appears random efficiency.

i see no point if such comparisons, if really curious, let's compare assembly generated 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, debug build. in release build, these 2 calls inlined , additional call in second function eliminated.

so... no difference, really.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -