Thread: if is faster than switch?

  1. #1
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594

    if is faster than switch?

    I was told by a teacher that a series of nested if statements is actually faster than a switch statement. According to a book he had read, a switch statement is only faster if there are 100 or more conditions. However, he was unsure of the reasoning behind it.

    Can anyone verify/explain why this is true or not true?

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    I know that if statments are faster then switch statments. I have tested it in a loop with some conditions and if statments went faster. I am not sure if 100 conditions or more switch would be faster then if.

    Hope that helps!

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    i heard somewhere that if you have only a few switch statements, that it just treats it as a if-else if-else if-else sort of thing, but if you have a lot of cases, it actually jumps to the case position

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    I think wether switch is slower than nested if statements or not it's ofcourse much better and it worth using it even if it makes the prog. bit slower

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Use the one in the right circumstances to make your life easier.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    of course, I'm not going to change my habitst to always use if's. I was just curious as to why. You would think that a switch would be set up in some sort of conditional array, making every test like a single if, but apparently that's not the case.

    thanks guys!

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    If you have a switch like this:

    Code:
    case 3:
      ...
    case 5:
      ...
    case 6:
      ...
    or any reasonably contiguous sequence then the compiler can generate something like this:

    Code:
    static const code_ptr[] = { 0x4534, 0x0000, 0x5464, 0x5734 };
    
    if (switch_value >= 3 && switch_value <= 6)
    	goto code_ptr[switch_value - 3]
    which can be many orders of magnitude faster than comparing each value.

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    interesting, thank you very much anonytmouse. I always wondered how a switch looked underneath.

  9. #9
    If you have to optimize you conditionals, you either have way too many of them (1% of the time) or something else is wasting clock cycles. I don't think you should worry yourself over something like this.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    It depends alot on the processor, assembly output, and data in the cache. Many compilers, however, generate the same code for switch statements and if statements.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If someone is interested, I assembled the output of this program in Visual C++. The Switch uses some kind of calculation to get a jump address.
    Code:
    int main()
    {
    	int Alpha = 0;
    
    	//If
    	if(Alpha == 1)
    	{
    		Alpha = 0;
    	}
    	else if(Alpha == 10)
    	{
    		Alpha = 0;
    	}
    	else if(Alpha == 56)
    	{
    		Alpha = 0;
    	}
    	else if(Alpha == 128)
    	{
    		Alpha = 0;
    	}
    
    	//Switch
    	switch(Alpha)
    	{
    		case 1:
    			Alpha = 0;
    			break;
    
    		case 10:
    			Alpha = 0;
    			break;
    
    		case 56:
    			Alpha = 0;
    			break;
    
    		case 128:
    			Alpha = 0;
    			break;
    	}
    
    	return 0;
    }
    The IF-part got this output:
    Code:
    ; Line 6
    	cmp	DWORD PTR _Alpha$[ebp], 1
    	jne	SHORT $L268
    ; Line 8
    	mov	DWORD PTR _Alpha$[ebp], 0
    ; Line 10
    	jmp	SHORT $L274
    $L268:
    	cmp	DWORD PTR _Alpha$[ebp], 10		; 0000000aH
    	jne	SHORT $L270
    ; Line 12
    	mov	DWORD PTR _Alpha$[ebp], 0
    ; Line 14
    	jmp	SHORT $L274
    $L270:
    	cmp	DWORD PTR _Alpha$[ebp], 56		; 00000038H
    	jne	SHORT $L272
    ; Line 16
    	mov	DWORD PTR _Alpha$[ebp], 0
    ; Line 18
    	jmp	SHORT $L274
    $L272:
    	cmp	DWORD PTR _Alpha$[ebp], 128		; 00000080H
    	jne	SHORT $L274
    ; Line 20
    	mov	DWORD PTR _Alpha$[ebp], 0
    $L274:
    The SWITCH-part got this output:
    Code:
    ; Line 25
    	mov	eax, DWORD PTR _Alpha$[ebp]
    	mov	DWORD PTR -8+[ebp], eax
    	mov	ecx, DWORD PTR -8+[ebp]
    	sub	ecx, 1
    	mov	DWORD PTR -8+[ebp], ecx
    	cmp	DWORD PTR -8+[ebp], 127			; 0000007fH
    	ja	SHORT $L276
    	mov	eax, DWORD PTR -8+[ebp]
    	xor	edx, edx
    	mov	dl, BYTE PTR $L287[eax]
    	jmp	DWORD PTR $L288[edx*4]
    $L279:
    ; Line 27
    	mov	DWORD PTR _Alpha$[ebp], 0
    ; Line 28
    	jmp	SHORT $L276
    $L280:
    ; Line 31
    	mov	DWORD PTR _Alpha$[ebp], 0
    ; Line 32
    	jmp	SHORT $L276
    $L281:
    ; Line 35
    	mov	DWORD PTR _Alpha$[ebp], 0
    ; Line 36
    	jmp	SHORT $L276
    $L282:
    ; Line 39
    	mov	DWORD PTR _Alpha$[ebp], 0
    $L276:
    This was, of course, made in VC, so I'm not saying all compilers does it like this.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    135

    Re: if is faster than switch?

    Originally posted by skorman00
    I was told by a teacher that a series of nested if statements is actually faster than a switch statement. According to a book he had read, a switch statement is only faster if there are 100 or more conditions. However, he was unsure of the reasoning behind it.

    Can anyone verify/explain why this is true or not true?
    It's a sweeping generalisation that serves only to confuse. Ignore it.

    By the way, can you tell us which book it was that said this, and provide a direct quote?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was told by a teacher that a series of nested if statements is actually faster than a switch statement.
    My response to statements like this is: "Prove it." The language doesn't define speed for such things, so it's up to the implementation. The constrains on a switch are such that even a bad implementation can (in theory) create fast machine code. An if has leeway for doing more work and thus creating slower machine code than a switch in said bad implementation. So (in theory), on a stupid compiler a switch can be faster through luck of definition. On good optimizing compilers, one or the other will probably be faster, but the difference will be vanishingly small. So write the code that's clearest and don't worry about speed issues until you know for a fact that you have a problem.
    My best code is written with the delete key.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	mov	dl, BYTE PTR $L287[eax]
    	jmp	DWORD PTR $L288[edx*4]
    I think we need L287 and L288 to figure out whaqt is going on here?

  15. #15
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    By the way, can you tell us which book it was that said this, and provide a direct quote?
    no, he doesn't even remember where he read it. He only saw it as an interesting tid bit, he never worries about it himself.

    I always wanted to learn assembly, and with Magos' post, I see this as a perfect time to start

    would you be able to point me to a decent assembler/tutorial?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM