switch Statement Performance Consideration

Environment: General

Here is an example of a switch statement:

switch( parameter )
{
case 1: { ... } ; break;
case 2: { ... } ; break;
case 3: { ... } ; break;
......
default: { ... } ; break;
}

If one looks at the assembly code generated for this, one notices that there are two portions:

Portion 1:

contains a list of compares and jumps for each of the cases listed within the switch statement.

Portion 2:

Contains code that implements each of the cases.

The list in portion one is something like:

  • compare parameter with 1 , if equal jump to case 1 code start
  • compare parameter with 2 , if equal jump to case 2 code start
  • compare parameter with 3 , if equal jump to case 3 code start
  • and so on

    Note that, the list is in the order in which the cases have been listed.

    Looking at this, it is a better idea to have cases that have more chances of occurence to be put in the beginning than at the end so that, they don't have to go through unnecessary comparisons. This is mostly applicable where the switch statements are present in function bodies which are called often, for example, Window procedures, call back function etc. In such cases, cases which could happen often , like WM_PAINT, WM_MOUSEmessages, WM_KEYDOWN etc can be pushed up in the order.

    It certainly cannot be used when fall through is used for certain cases, but, in a general sense, it surely is a good programming practice.



  • Comments

    • http://www.oakleysunglassesoutc.com/ gxovkn

      Posted by http://www.oakleysunglassesoutc.com/ Mandyxwb on 03/29/2013 05:45am

      cheap ghd,Four p.m. 30 battalions of army weapons all over the Zhili Governor standard new army, the army of the Changxindian temporary residence, in accordance with the requirements of the Northern Army Staff, fifteen battalion will go tomorrow The Beiyang Army ground Cheung Xiaozhan barracks. Go to the station barracks accept reorganization army will become the eighth town, Changxindian ninth town ghd straightener will conduct a three-month reorganization. Three months is the time of the Tan Yan to the Northern Army, in the history of the Revolution from the explosion to the provinces ghd sale memory competing response, this intermediate lasted several months, the middle and not by foreign powers as interference events. But Tan Yansheng caution. Students a lot of the historical pattern of change ghd australia,hairstraighteneraul.com/" title="ghd hair straightener"ghd hair straightener can not be sure the attitude of foreign powers in the manner of the Chinese revolution - Japan although not qualified in this situation to fish in troubled waters, but Russia has become the most greedily countries countries foreign powers in China, but it is Boxer Incident disarray occupied the northeast, the history of it is because the Northeast and Japan to play a game, there is no guarantee that the Russians will not take the opportunity to south occupied the northeast.

      Reply
    • Creating a Business Blog That Increases Sales

      Posted by nelpexela on 11/20/2012 03:45am

      I was more than happy to seek out this web-site.I desired to thanks for some time because of this amazing read!! I definitely enjoying every little little it and I've you bookmarked to have a look at new stuff you blog post. Am I Allowed To just say what a relief to uncover an individual who really knows what theyre speaking about online. NFL Jerseys NFL Jerseys

      Reply
    • Tube in the unstinting itsy-bitsy boots the storm women's winter geezer of the hour

      Posted by audifidergori on 11/05/2012 06:47pm

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Down jackets women this year what's unfamiliar

      Posted by melaSeime on 11/05/2012 06:43pm

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Most women like the the go heap Celine Celine grin package deal

      Posted by PSYGIESEE on 11/05/2012 10:22am

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • As long as a person knows where to go, would give way to him allover the world.

      Posted by GexyProonry on 11/04/2012 07:24pm

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Most women like the dernier cri pack Celine Celine grin packet

      Posted by PSYGIESEE on 11/04/2012 07:14pm

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Down jackets women this year what's modish

      Posted by melaSeime on 11/04/2012 06:55pm

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Most women like the fashion away Celine Celine grin case

      Posted by PSYGIESEE on 11/03/2012 04:44am

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Tube in the luxurious crusty boots taste women's winter hero

      Posted by audifidergori on 11/03/2012 04:40am

      north face outlet north face coats north face sale north face jackets north face outletnorth face sale north face sale north face uk アグ ugg ブーツ ugg ブーツ 2012 north face clothing north face sale north face online north face uk

      Reply
    • Loading, Please Wait ...

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

    • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds