On 03/18/2010 05:22 AM, Jae Hyuk Kwak wrote: > On Wed, Mar 17, 2010 at 1:04 PM, Michael Meissner > <meiss...@linux.vnet.ibm.com> wrote: >> Note, that many hash tables are computed by the modulus operation, which is >> often fairly expensive (and on machines without a hardware divide unit, >> requiring a function call). I would expect many switch statements would slow >> down if you switch to a hash operation that used modolus. >> > > I agree that the cost of modulation can be high, but it can be even > higher if we use a bunch of "else if". > Consider the situation that a program has about 400 cases on a single > switch statement. > > The cost of modulation will be fixed price so that there should be a > certain point that the price bis lower than else if statements. > > If jump table is possible, it can be a choice, but jump table is not > always feasible depending on the values on "case".
You don't have to speculate, you can measure. gcc has the "labels as values" extension which you can use to simulate a hash-based switch statement. So, please try it and see if it's faster. Andrew.