
Switch Statement Usage
21
22007E/0—November 1999
AMD Athlon™ Processor x86 Code Optimization
Switch Statement Usage
Optimize Switch Statements
Switch statements are translated using a variety of algorithms.
The most common of these are jump tables and comparison
chains/trees. It is recommended to sort the cases of a switch
statement according to the probability of occurrences, with the
most probable first. This will improve performance when the
switch is translated as a comparison chain. It is further
recommended to make the case labels small, contiguous
integers, as this will allow the switch to be translated as a jump
table.
Example 1 (Avoid):
int days_in_month, short_months, normal_months, long_months;
switch (days_in_month) {
case 28:
case 29: short+; break;
case 30: normal+; break;
case 31: long+; break;
default: printf ("month has fewer than 28 or more than 31
days\n");
}
Example 2 (Preferred):
int days_in_month, short_months, normal_months, long_months;
switch (days_in_month) {
case 31: long+; break;
case 30: normal+; break;
case 28:
case 29: short+; break;
default: printf ("month has fewer than 28 or more than 31
days\n");
}
Use Prototypes for All Functions
In general, use prototypes for all functions. Prototypes can
convey additional information to the compiler that might
enable more aggressive optimizations.
Summary of Contents for Athlon Processor x86
Page 1: ...AMD Athlon Processor x86 Code Optimization Guide TM...
Page 12: ...xii List of Figures AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 16: ...xvi Revision History AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 202: ...186 Page Attribute Table PAT AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 252: ...236 VectorPath Instructions AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 256: ...240 Index AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...