switch Statement Performance Consideration | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 23, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

  • CodeGuru Logo

    CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

    Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.