Sign in to follow this  
godsenddeath

overhead difference between virtual functions and function pointers

Recommended Posts

I've been looking over different Finite State Machines that i've encountered, and I've noticed that child classes and function pointers are used in the same ways, I was wondering, in light of the tables that must be set up when using virtual functions, and ignoring the fact that polymorphism allows a lot more flexability, which performs better?

Share this post


Link to post
Share on other sites
Polymorphism allows more flexibility? Function pointers are data and can be changed at runtime!

That brings us on to the answer to your question, cache! (Lets assume the function pointer is never NULL and therefore needs no IF testing) In both cases we need to fetch the pointer we are going to jump to. In the case of function pointers where is that data stored, if it's already in the data cache well and good, execution should be incrediably fast! If not.. well... not so fast. The same applies to the vtable, your vtable will reside in code memory somewhere probably a long way from the code you are currently executing, so likely a cache miss? Probably unless you've used that particular vtable for some other jump recently.

Is vtable fetched by the instruction cache or data cache? I must admit I don't know, I would have thought data cache however.

Actually following the pointer when retrieved will be the same speed for both methods.

The other major factor influencing execution speed is whether the function you're jumping to is in the instruction cache, if it's close by in the code there's a chance it's fetched some of it, otherwise it depends if you've recently visited this code.

So which is faster I would think is entirely due to your particular circumstances. In general I prefer C++ design so would only use function pointers when the might want to be changed at runtime, humm... like in state machines.

Share this post


Link to post
Share on other sites
Function pointers use one less indirection. As such, they are at least as fast as using virtual functions. This doesn't mean they're faster—the exact speed difference depends on how much code you execute for every function call. Also consider the fact that you can implement an FSM with a switch, which may be faster in some cases due to cache locality of the branches.

Keep an easily refactored architecture, and profile.

Quote:
Original post by Martin
Polymorphism allows more flexibility? Function pointers are data and can be changed at runtime!
Booleans are also data, and can also be changed at runtime, but they're not advertised as more flexible than object-oriented polymorphism. You can also implement function pointers by using a virtual operator() on a class.

The real difference between object-oriented polymorphism and function pointers, in C++, is that function pointers cannot carry data—they're not closures. In that regard, you can implement "the function which adds X to its argument" (for a value of X determined at runtime) using object-oriented polymorphism, but not with function pointers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this