c++ - Is operation if(!is_active) faster than if(is_active == false)? -


i confuse between 2 operation:

if(!is_active) {   here... } 

and

if(is_active == false) {   here... } 

which faster another? , if faster, why faster. can explain in bit operator 0 , 1.

both equivalent. can test using -s option, produces assembler output file.s. gcc on amd64 example

file.cpp:

void f() {     bool is_active = false;     if(!is_active) { dosomething(); }      if(is_active == false) { dosomething(); } } 

file.s:

...     movzbl  -1(%rbp), %eax     xorl    $1, %eax     testb   %al, %al     je  .l3     call    _z11dosomethingv .l3:     movzbl  -1(%rbp), %eax     xorl    $1, %eax     testb   %al, %al     je  .l2     call    _z11dosomethingv .l2: ... 

you can see code same both instances.

update charles bailey's comment including compiler optimization -o2

file.cpp:

extern bool is_active;  void f() {     if(!is_active) { dosomething(); } }  void g() {     if(is_active == false) { dosomething(); } } 

file.s:

    cmpb    $0, is_active(%rip)     je    .l4     rep     ret     .p2align 4,,10     .p2align 3 .l4:     jmp    _z11dosomethingv ...     cmpb    $0, is_active(%rip)     je    .l7     rep     ret     .p2align 4,,10     .p2align 3 .l7:     jmp    _z11dosomethingv 

the produced assembler code different time, expected it's same both if statements.


Comments