[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Texmacs-dev] Efficiency of C++ switch-statements
From: |
Bob McElrath |
Subject: |
Re: [Texmacs-dev] Efficiency of C++ switch-statements |
Date: |
Mon, 10 Nov 2003 10:28:35 -0800 |
User-agent: |
Mutt/1.5.4i |
Joris van der Hoeven address@hidden wrote:
>
> Hi *,
>
> Can anyone provide me with some information on how well
> the C++ switch-statement is being optimized?
>
> I am interested in the following situation: I have 150 built-in
> TeXmacs tags and a few places in the code where some action has
> to be undertaken which is different for about 10 tags and
> the same for the remaining 140 tags. So I use a switch with
> a "default:". My question is: does C++ use table lookup for
> immediately jumping to the right location (the tables would
> clearly be quite sparse)?
Yes, it does use a table.
This page is useful:
http://www.eventhelix.com/RealtimeMantra/Basics/CToAssemblyTranslation3.htm
...it says compliers sometimes use a binary search if the table would be
sparse.
> Another question: if I do not put the cases in the same order
> as the enumeration, does this alter the efficiency?
If I remember correctly, switch used to essentially be a <= so you had
to make sure they were in order or the wrong case would be executed. (I
just tested this, and it is not true -- either I remember wrong or this
is a C/C++ difference) I don't know if this varies from compiler to
compiler.
Also consider using the STL hash_map object, which gives you O(1)
lookups and does not waste space when your table is sparse.
Cheers,
Bob McElrath [Univ. of California at Davis, Department of Physics]
signature.asc
Description: Digital signature