Справочник Пользователя для AMD 250
Chapter 2
C and C++ Source-Level Optimizations
25
Software Optimization Guide for AMD64 Processors
25112
Rev. 3.06
September 2005
2.10
SWITCH and Noncontiguous Case Expressions
Optimization
Use
if-else
statements in place of
switch
statements that have noncontiguous case expressions.
(Case expressions are the individual expressions to which the single
switch
expression is compared.)
Application
This optimization applies to:
•
32-bit software
•
64-bit software
Rationale
If the case expressions are contiguous or nearly contiguous integer values, most compilers translate
the
the
switch
statement as a jump table instead of a comparison chain. Jump tables generally improve
performance because:
•
They reduce the number of branches to a single procedure call.
•
The size of the control-flow code is the same no matter how many cases there are.
•
The amount of control-flow code that the processor must execute is the same for all values of the
switch
expression.
However, if the case expressions are noncontiguous values, most compilers translate the
switch
statement as a comparison chain. Comparison chains are undesirable because:
•
They use dense sequences of conditional branches, which interfere with the processor’s ability to
successfully perform branch prediction.
successfully perform branch prediction.
•
The size of the control-flow code increases with the number of cases.
•
The amount of control-flow code that the processor must execute varies with the value of the
switch
expression.