Re: How do Visual C++ compiler optimize switch & cases?
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Sun, 19 Jun 2005 17:02:53 -0700
He Shiming wrote:
> Hi,
>
> I have this switch block in my program, and it has over 200 cases. I'm
> wondering if I should worry about its performance. Is iterating over
> all the case labels a time-consuming job?
>
> I know from others that if the compiler is "smart enough", it could
> sort the case labels to make label-addressing faster. Is that what
> Visual C++ compiler has been doing? In general, do I need to worry
> about the performance of such structured code?
In an optimized build, VC++ will generate a mixture of up to three different
techniques:
1. Sequential if/else logic (for switches with very few cases - e.g. 2).
2. Table lookup (for switches with one or more ranges of consequtive case
values).
3. Tree search (optimized if/else by sorting the tags and then generating a
binary search of the tag space).
The compiler will mix these techniques: a switch with mixed labels and
contiguous ranges will typically use the binary tree logic to select from
indivudal cases or table-based dispatch.
-cd
.
- Follow-Ups:
- Re: How do Visual C++ compiler optimize switch & cases?
- From: He Shiming
- Re: How do Visual C++ compiler optimize switch & cases?
- References:
- How do Visual C++ compiler optimize switch & cases?
- From: He Shiming
- How do Visual C++ compiler optimize switch & cases?
- Prev by Date: Re: How do Visual C++ compiler optimize switch & cases?
- Next by Date: Re: Timer
- Previous by thread: Re: How do Visual C++ compiler optimize switch & cases?
- Next by thread: Re: How do Visual C++ compiler optimize switch & cases?
- Index(es):
Relevant Pages
|