Search code examples
c++cperformancereadability

Does writing boolean conditions "in full" compromise performance?


For the sake of readability, I like to write boolean conditions "in full" in C/C++, e.g.:

if (foo == true) instead of if (foo) and if (foo == false) instead of if (!foo).

Does this compromise performance?


Solution

  • As far as straight C is concerned, the two forms may result in different machine code, which may affect performance. Whether that performance difference matters will depend on what the code is doing and what your performance requirements are.

    Using gcc 4.1.2 on SLES 10, I get slightly different machine code for if ( foo == false ) vs if ( !foo ); given the following source code:

    if ( foo == false )
    {
      printf( "foo is false\n" );
    }
    
    if ( !foo )
    {
      printf( "foo is 0\n" );
    }
    

    I get the following machine code (gcc -S -std=c99 -pedantic -Wall -Werror):

            movb    $0, -1(%rbp)      ;; foo = false
            cmpb    $0, -1(%rbp)      ;; if ( foo == false )
            jne     .L6               ;; {
            movl    $.LC2, %edi       ;;   
            call    puts              ;;    printf( "foo is false\n" );
    .L6:                              ;; }
            movzbl  -1(%rbp), %eax    ;; if ( !foo )
            xorl    $1, %eax   
            testb   %al, %al
            je      .L8               ;; {
            movl    $.LC3, %edi       ;;   
            call    puts              ;;    printf( "foo is 0\n" );
    .L8:                              ;; }
            movl    $0, %eax
    

    For this particular platform, compiler, code, and set of compiler options, if (!foo) may wind up being a little slower than if ( foo == false )1.

    Now, for the real question: does that performance difference (if it exists) matter? For this particular program, absolutely not; the test occurs once over the lifetime of the program, and the time spent doing I/O will be considerably greater than the time spent performing the test. In a different program where that test is being performed thousands or millions of times in a tight loop that's CPU bound, then it can matter quite a bit.

    Code for readability first; the if ( foo ) and if ( !foo ) forms tend to be idiomatic for simple Boolean tests, and that's what most C and C++ programmers will expect to see. Having said that, here's absolutely nothing wrong with writing if ( foo == TRUE ) and if ( foo == FALSE ) if you feel it conveys the intent of the code better (or if you decide to define TRUE as 0 and FALSE as 1 for whatever reason).

    But don't make that decision based on performance unless:

    1. You are failing to meet a hard performance requirement;
    2. You have optimized your algorithm and data structures appropriately; and
    3. Profiling shows that particular statement is the remaining bottleneck.


    1. Or not; it depends on how many cycles xorl and testb require vs cmpb and how much faster it is access register %eax directly than to compute -1(%rbp) and access the resulting location; for all I know it's a wash.