Does the order of case in Switch statement can vary the performance?

Posted by Bipul on Stack Overflow See other posts from Stack Overflow or by Bipul
Published on 2010-05-12T03:39:36Z Indexed on 2010/05/12 3:44 UTC
Read the original article Hit count: 232

Filed under:
|

Let say I have a switch statement as below

Switch(alphabet){

case "f":
//do something
break;

case "c":
//do something
break;

case "a":
//do something
break;

case "e":
//do something
break;
}

Now suppose I know that the frequency of having Alphabet e is highest followed by a, c and f respectively. So, I just restructured the case statement order and made them as follows.

Switch(alphabet){

case "e":
//do something
break;

case "a":
//do something
break;

case "c":
//do something
break;

case "f":
//do something
break;
}

Will the second Switch statement better perform(means faster) than the first switch statement? If yes and if in my program I need to call this switch statement say many times, will that be a substantial improvement? Or if not in any how can I use my frequency knowledge to improve the performance?

Thanks

© Stack Overflow or respective owner

Related posts about switch-case

Related posts about c#