Re: Maximizing speed of a switch block
Mycroft Holmes wrote:
Is there any reason why a function-pointer jump would be faster than an
In the best case, a switch can check if the argument is in a certain range
and then dispatch that range via a table. In the worst case, it generates a
series of if-then-else constructs which mess up dispatch speed.
BTW: I don't buy the argument that the enum is "conceptually public", at
least nothing in the example makes it that. If you make it private, you can
restrict the range of values it can take (encapsulation). You can then use
this knowledge to either build a table to look up the according function on
demand or simply look up and store the function when it is set. That way,
you could switch between both implementations quickly.
Note: if you have multiple such functions, defining a struct X_ops with all
the operations and then just switching that structure on demand might make
even more sense. Basically that structure would be like a vtable, only that
it can be changed at runtime.
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932