• Advertisement
Sign in to follow this  

Performance of Virtual Functions

This topic is 3956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I heard that virtual functions slow a program down. Is this a significant decrease of performance? I currently plan to use an overarching class that I inherit many things from, and am using virtual functions so that if a child doesn't have any specific change to a function, it can not rewrite its own. Most of the children have at least 2-3 overriding functions, and I want to make sure that this won't have a negative impact. These functions are not things called every frame (game), either, just once every second or two. Thanks

Share this post


Link to post
Share on other sites
Advertisement
The absolute time cost of virtual functions is irrelevant when optimizing a program.

What is important is the relative cost: are virtual functions slower or faster than alternatives? The answer is simple: if you need dynamic binding of behaviour, then virtual functions are the fastest solution available. If you don't need dynamic binding of behaviour, then virtual functions are quite possibly useless overhead.

So, does your program need dynamic binding?

Share this post


Link to post
Share on other sites
How many calls are we talking about per second? If less than 10000, then I suggest you follow Promit's advice and ignore it for now. If you have more then it might present a noticeable, but probably insignificant performance hit. In that case you should try analyzing the performance of your program using a profiler and then decide whether it really is a problem (usually it still isn't). Virtual functions can be a bottle neck if you call one for every triangle rendered, but normally they present an insignificant performance drop compared to other issues you could spend your time optimizing.

Share this post


Link to post
Share on other sites
Alright, thanks everyone who replied. I'm just calling them every few seconds (max), and wanted to make sure it wouldn't be a problem down the road. Thanks a lot!

Share this post


Link to post
Share on other sites
The advice that I've heard that's helped me the most when it comes to everything is: "get everything working first, worry about performance when it actually loads."

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement