"Checking" speed (if,switch)

1
2
3
4
5
//Which of these are faster:
if (stat1 && stat2) {}    //1
if (stat1) {if(stat2){} } //2
//I think 2nd one is faster because it stops the further checking if stat1 is
//false, while the first code has to check both 

And:
1
2
3
4
5
6
7
8
9
10
11
12
13
//Which of these are faster:
if (stat==1) {} else
if (stat==2) {} else
if (stat==3) {}
//or
switch (stat)
{
    case 1: {} break
    case 2: {} break
    case 3: {} break

}
//Again I think if is faster 

Am I right here or not?
Thankyou
First question: No, unless you overloaded the '&&' operator.

C++ uses "lazy evaluation" a.k.a. short-circuiting. In short, if it finds a value that determines the "total", it stops. For example, if (a && b && c) is 0 if one of the three is false, regardless of what the others are. Thus, if a is false, it can stop right there. If a is true, it checks b, and so on. The other way around, if (a || b || c) short-circuits when it finds any true, because then the complete statement evaluates as true.

For if vs switch: it depends, and I'm not nearly experienced enough to know on what. I do know that for "easy" if-elseif/switch constructs, a decent compiler will generate the same code. There was an article posted here on micro-optimizations not too long ago. Mail codekiddy if you want it. However, I don't think that for basic cases you'll get any extra juice out of using "the best one".

[edit]

Found the topic and article:
http://www.cplusplus.com/forum/lounge/58994/
The part on if/switch/jtables is in the first few pages.

Be careful when reading it; much of it is outdated (or platform/compiler-dependent). T
Last edited on
The first two are exactly identical (EDIT: Unless operator&& is overloaded, as Gaminic correctly pointed out. But seeing as switch() is considered, I am guessing it doesn't apply)

1
2
3
4
5
6
7
8
9
volatile bool stat1, stat2;
volatile int sink;
int main()
{
    if(stat1 && stat2)
    {
            sink = 1;
    }
}
        movzbl  stat1(%rip), %eax
        testb   %al, %al
        je      .L2
        movzbl  stat2(%rip), %eax
        testb   %al, %al
        je      .L2
        movl    $1, sink(%rip)
.L2:



1
2
3
4
5
6
7
8
9
10
volatile bool stat1, stat2;
volatile int sink;
int main()
{
    if (stat1)
        if(stat2)
        {
            sink = 1;
        }
}

        movzbl  stat1(%rip), %eax
        testb   %al, %al
        je      .L2
        movzbl  stat2(%rip), %eax
        testb   %al, %al
        je      .L2
        movl    $1, sink(%rip)
.L2:



The second two are also identical, but in my test, where stat is volatile int, the compiler is not required to read from stat three times in the case of switch so it is slightly faster.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
volatile int stat, sink;
int main()
{
    if (stat==1) { sink = 1; } else
        if (stat==2) { sink = 2; } else
            if (stat==3) { sink = 3; }
}
        movl    stat(%rip), %eax
        cmpl    $1, %eax
        je      .L6
        movl    stat(%rip), %eax
        cmpl    $2, %eax
        je      .L7
        movl    stat(%rip), %eax
        cmpl    $3, %eax
        je      .L8
.L3:
        xorl    %eax, %eax   // this is end of main()
        ret
.L8:
        movl    $3, sink(%rip)
        jmp     .L3
.L6:
        movl    $1, sink(%rip)
        jmp     .L3
.L7:
        movl    $2, sink(%rip)
        jmp     .L3
.LFE0:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
volatile int stat, sink;
int main()
{
    switch (stat)
    {
        case 1: { sink = 1; } break;
        case 2: { sink = 2; } break;
        case 3: { sink = 3; } break;
    }
}
        movl    stat(%rip), %eax
        cmpl    $2, %eax
        je      .L4
        cmpl    $3, %eax
        je      .L5
        subl    $1, %eax
        je      .L7
.L2:
        xorl    %eax, %eax // this is end of main()
        ret
.L7:
        movl    $1, sink(%rip)
        jmp     .L2
.L5:
        movl    $3, sink(%rip)
        jmp     .L2
.L4:
        movl    $2, sink(%rip)
        jmp     .L2
        .cfi_endproc
.LFE0:


Don't worry about such things. Worry about choosing efficient algorithms first, then about code clarity, and then profile.

GCC 4.6.2 was used for the sample compiler output.
Last edited on
I was just wondering. Thanks people.
Topic archived. No new replies allowed.