Re: Which control structure is faster?



On Thu, 2009-02-19 at 17:11 +0100, Dimitri Holz wrote:
> Which control structure is faster inside a loop (for, while or do-while)?

switch is probably faster because the compiler can use a lookup table.
But the compiler maybe optimizes the if version to do the same as the
switch version anyway. That's only theoretical, though. If you want to
know for sure, you should profile your code.

> 1)----------------------------------------------------------
> 
>         switch(x)
>         {
>                case 1:
>                {
>                    .....    ;
>                    break;
>                 }
>                 case 2:
>                 {
>                    ....    ;
>                    break;
>                 }
>          }
> 
> 2)---------------------------------------------------------
> 
>  
>           if(x = = 1)
>           {
>                 ........ ;
>                 break;
>           }
>           if(x = = 2)
>           {
>                 ........;
>                 break;
>           }

Armin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]