texmacs-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Texmacs-dev] Efficiency of C++ switch-statements


From: Joris van der Hoeven
Subject: Re: [Texmacs-dev] Efficiency of C++ switch-statements
Date: Mon, 10 Nov 2003 22:58:15 +0100 (CET)

> > 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.

Thanks for the pointer. What does g++ do?

> > 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.

This might also cause some overhead. I would like something which
just does table lookup. If necessary, I can put 150 cases,
but I would prefer to know how many cases are necessary and
whether the order is really important (for g++).





reply via email to

[Prev in Thread] Current Thread [Next in Thread]