c+ - Est plus rapide que 

Mots clés : c++cperformanceassemblyrelational-operatorsc++c

meilleur 4 Réponses c+ - Est plus rapide que 

vote vote

95

    if (a < b) {         // Do something 1     } 
    mov     eax, DWORD PTR [esp+24]      ; a     cmp     eax, DWORD PTR [esp+28]      ; b     jge     .L2                          ; jump if a is >= b     ; Do something 1 .L2: 
    if (a <= b) {         // Do something 2     } 
    mov     eax, DWORD PTR [esp+24]      ; a     cmp     eax, DWORD PTR [esp+28]      ; b     jg      .L5                          ; jump if a is > b     ; Do something 2 .L5: 
      Latency   Throughput Jcc     N/A        0.5 
        fld     QWORD PTR [esp+32]         fld     QWORD PTR [esp+40]         fucomip st, st(1)              ; Compare ST(0) and ST(1), and set CF, PF, ZF in EFLAGS         fstp    st(0)         seta    al                     ; Set al if above (CF=0 and ZF=0).         test    al, al         je      .L2         ; Do something 1 .L2:          fld     QWORD PTR [esp+32]         fld     QWORD PTR [esp+40]         fucomip st, st(1)              ; (same thing as above)         fstp    st(0)         setae   al                     ; Set al if above or equal (CF=0).         test    al, al         je      .L5         ; Do something 2 .L5:         leave         ret 
vote vote

85

Comparison     Subtraction ----------     ----------- A < B      --> A - B < 0 A = B      --> A - B = 0 A > B      --> A - B > 0 
Comparison     Subtraction  Carry Bit  Zero Bit ----------     -----------  ---------  -------- A < B      --> A - B < 0    0          0 A = B      --> A - B = 0    1          1 A > B      --> A - B > 0    1          0 
;; Implementation of "if (A < B) goto address;" cmp  A, B          ;; compare A to B bcz  address       ;; Branch if Carry is Zero to the new address 
;; Implementation of "if (A <= B) goto address;" cmp A, B           ;; compare A to B bcz address        ;; branch if A < B bzs address        ;; also, Branch if the Zero bit is Set 
vote vote

78

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

64

int compare_strict(double a, double b) { return a < b; } 
int compare_loose(double a, double b) { return a <= b; } 
int compare_loose(double a, double b) { return ! (a > b); } 

Questions similaires