janjust
janjust

Reputation: 826

Performance in conditional statements

I noticed that a code I'm working on uses conditionals like:

if(A != val) {
   // code B
}
if(B != val) {
  // code A
}

But to me, reading the same code like below is much easier. Maybe a personal preference.

if( B == val) {
   //code B
}
if( A == val) {
  //code A
}

This is a very latency sensitive code so is there a performance difference between the two? Is there a performance difference between != or == ? or > or < for that matter? Also, I realize that this code leaves room for a 3rd condition though I'm pretty sure the code leaves only 2 paths possible so an if/else is more appropriate.

Much appreciated.

Upvotes: 0

Views: 213

Answers (2)

mik1904
mik1904

Reputation: 1365

Here is the assembly code when in the condition you use !=, note that I added a printf inside the if statement in order to have a better output from gdb:

   0x0000000000400526 <+0>:     push   rbp
   0x0000000000400527 <+1>:     mov    rbp,rsp
   0x000000000040052a <+4>:     sub    rsp,0x10
   0x000000000040052e <+8>:     mov    DWORD PTR [rbp-0xc],0x0
   0x0000000000400535 <+15>:    mov    DWORD PTR [rbp-0x8],0x1
   0x000000000040053c <+22>:    mov    DWORD PTR [rbp-0x4],0x1
   0x0000000000400543 <+29>:    mov    eax,DWORD PTR [rbp-0xc]
   0x0000000000400546 <+32>:    cmp    eax,DWORD PTR [rbp-0x4]
   0x0000000000400549 <+35>:    **je**     0x400555 <main+47>
   0x000000000040054b <+37>:    mov    edi,0x4005f4
   0x0000000000400550 <+42>:    call   0x400400 <puts@plt>
   0x0000000000400555 <+47>:    mov    eax,DWORD PTR [rbp-0x8]
   0x0000000000400558 <+50>:    cmp    eax,DWORD PTR [rbp-0x4]
   0x000000000040055b <+53>:    **je**     0x400567 <main+65>
   0x000000000040055d <+55>:    mov    edi,0x4005f4
   0x0000000000400562 <+60>:    call   0x400400 <puts@plt>
   0x0000000000400567 <+65>:    mov    eax,0x0
   0x000000000040056c <+70>:    leave  
   0x000000000040056d <+71>:    ret

While this is for ==:

0x0000000000400526 <+0>:    push   rbp
0x0000000000400527 <+1>:    mov    rbp,rsp
0x000000000040052a <+4>:    sub    rsp,0x10
0x000000000040052e <+8>:    mov    DWORD PTR [rbp-0xc],0x0
0x0000000000400535 <+15>:   mov    DWORD PTR [rbp-0x8],0x1
0x000000000040053c <+22>:   mov    DWORD PTR [rbp-0x4],0x1
0x0000000000400543 <+29>:   mov    eax,DWORD PTR [rbp-0x8]
0x0000000000400546 <+32>:   cmp    eax,DWORD PTR [rbp-0x4]
0x0000000000400549 <+35>:   **jne**    0x400555 <main+47>
0x000000000040054b <+37>:   mov    edi,0x4005f4
0x0000000000400550 <+42>:   call   0x400400 <puts@plt>
0x0000000000400555 <+47>:   mov    eax,DWORD PTR [rbp-0xc]
0x0000000000400558 <+50>:   cmp    eax,DWORD PTR [rbp-0x4]
0x000000000040055b <+53>:   **jne**    0x400567 <main+65>
0x000000000040055d <+55>:   mov    edi,0x4005f4
0x0000000000400562 <+60>:   call   0x400400 <puts@plt>
0x0000000000400567 <+65>:   mov    eax,0x0
0x000000000040056c <+70>:   leave  
0x000000000040056d <+71>:   ret 

As you may notice the only difference is the usage of je (jump if equal) or jne (jump if not equal) so in terms of performance you could say that it' exactly the same

Upvotes: 3

nucleon
nucleon

Reputation: 1158

You will have a conditional jump in your assembler code whether you check for equality or any kind of unequality. Performance difference should be marginal.

Though it can be beneficial to write branchless code, see e.g. Why is it faster to process a sorted array than an unsorted array?.

Upvotes: 0

Related Questions