Switch Statement - Compilation

Compilation

If the range of input values is identifiably 'small' and has only a few gaps, some compilers that incorporate an

Optimizing compilers such as GCC or Clang may compile a switch statement into either a branch table or a binary search through the values in the cases. A branch table allows the switch statement to determine with a small, constant number of comparisons which branch to execute without having to go through a list of comparisons, while a binary search takes only a logarithmic number of comparisons, measured in the number of cases in the switch statement.

Normally, the only method of finding out if this optimization has occurred is by actually looking at the resultant assembly or machine code output that has been generated by the compiler (and is therefore seldom, if ever, done by HLL programmers). The first 'C' example below would be eligible for this kind of optimization if the compiler supported it (the range '0' through '9' with zero gaps without a defined case label).

Read more about this topic:  Switch Statement

Famous quotes containing the word compilation:

    The United States Constitution has proved itself the most marvelously elastic compilation of rules of government ever written.
    Franklin D. Roosevelt (1882–1945)