C++ question here. I've got a system where I am likely to have 100s of small-subclasses of the given superclass. All of them may have a "foo" method that does something. Or... I am likely to have one class by having an integer known as "type" and employ a huge switch statement to determine how to proceed after i foo.

Performance has become a consideration here. Very important.

Now you ask ,, do you know the performance benefits/penalties of utilizing changing statement versus. letting C++ get it done through the vftable? Basically get it like a switch statement, I'm able to place the generally happening foo's up towards the top of the switch statement and also the less frequent ones at the end, hopefully shortcutting the comparison. Looking to get an impact such as this using the vftable is certain to be compiler dependent even when I'm able to learn how to get it done...

However, my code will be a lot simpler to cope with without these ugly switch claims.

Basically get it like a switch statement, I'm able to place the generally happening foo's up towards the top of the switch statement and also the less frequent ones at the end, hopefully shortcutting the comparison.

A switch statement is usually put together to some jump table as opposed to a block of if-else conditionals as the question suggests. Used, the virtual table and also the switch jump table must have similar performance, though test if you are really concerned.

The compiler determines the way the switch claims are handled, but you will find a couple of fundamental techniques they will use.

  1. if-else binary-sort: The comparison is performed as a number of if-else however in a binary-sort like fashion, performance is thus similar to research inside a map of N products
  2. jump table: when the products are close enough together a table of addresses is going to be created. Research will be in constant time

In which the situation claims are situated within the switch statement does not matter either in situation.

Virtual functions come with an overhead in comparison to direct call. It calls for one more offset and pointer research. For basically probably the most extreme performance factors this price is minimal. When evaluating to some switch the overhead isn't within the virtual research, however the function call itself. So changing statement that merely calls functions in each situation will work essentially just like virtual functions.

So basically the "dispatch semantics" of the switch statement (with jump table) in comparison to some virtual function call are nearly irrelevant. If all of your "foo" techniques are relatively small , could be inlined the switch statement will begin to perform better. Another benefit of switch is you can put common code prior to the switch and obtain better register/stack optimizations.

However, there's a substantial maintenance overhead. This ought to be most of your concern at this time. Why? Since the performance bottle-neck inside your code isn't likely the switching login, or perhaps the function calls, but another thing. Before you fix that another thing there's no reason in addressing these low-level performance issues. So stay with whichever provides more maintainable code right now.

Towards the other solutions here' would add two more.

1) It's harder and fewer common for any compiler to do classic optimizations (including enregistration) across an online function call interface than across situation labeled claims inside a switch statement in one function.

2) Any performance difference within the dispatch is extremely depedendent around the processor's branch conjecture hardware. A virtual function call target address (and return) might be properly predicted and also have minimal performance overhead within the pipeline of the modern out-of-order processor.

When the performance of the operation really matters, you need to check it out for both and measure it, poor the actual system.

Happy hacking!

Vtable ought to be faster in almost all cases, but when performance is really critical, the best factor to request is as simple as just how much.

Vtable call is triple indirection (three memory accesses to find the target CALL address). Cache misses shouldn't be an problem if there are many calls. So, it's roughly 2-3 switch label evaluations (although the latter offer less opportunity for CPU cache miss, but less for pipe usage).

You need to absolutely not depend on anything I stated here, and test drive it by using true performance dimensions in your target architecture.