Jump to content
  • Advertisement
Sign in to follow this  
DvDmanDT

Virtual methods?

This topic is 4834 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'm creating an RTS.. Now, in a worstcase scenario, I'll have 100000+ units/buildings/other things that needs to update.. Chanses are pretty big they'll have very much in common, and that 95% of them will use the save moves and stuff, but a few will need special code.. So I was thinking about a baseclass with virtual methods, and the default stuff coded there, and then the special ones would overload it.. It's also not completely impossible that I can live without those 5% with special code (at the moment I'm not really sure for what it could be used, but I think there could be cases).. My question is, would it affect my performance much to use a virtual method instead of a normal one? And take in to account that all my units/buildings must update on each frame, at a framerate of 50fps.. So my question is: How much slower is virtual methods when used in a pretty large scale?

Share this post


Link to post
Share on other sites
Advertisement
Virtual functions will be as fast or faster than any mechanism you choose to use to get the same functionality and tend to be less error prone to boot.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Virtual functions will be as fast or faster than any mechanism you choose to use to get the same functionality and tend to be less error prone to boot.


It's not that simple -- it can depend a lot on how your data is organized. The problem is thrashing the instruction and data caches. If your objects are in random order (as far as type is concerned) then every object is likely to require reloading of both the I$ and D$. On the other hand, if objects of the same type are grouped together both in terms of memory location and member function execution, then the caches are used much more efficiently.

You can do it two ways:
  1. Keep the 100000 objects sorted by type (and/or by address) and go through the list calling virtual functions.
  2. Have a "manager" for each type that accesses all of the objects of the type it controls. The cost of sorting may be reduced and the cost of virtual functions and their overhead (however minimal) could be avoided.
I recommend #1 as the first approach, and then trying #2 as a possible optimization.

Share this post


Link to post
Share on other sites
Well, say I only have one class then. Everyone will do the same thing. So I don't need the virtual keyword.. So I can iterate through the list in x ms.. My question is, how much slower it would become if I just added that 'virtual' keyword?

Share this post


Link to post
Share on other sites
Quote:
Original post by DvDmanDT
Well, say I only have one class then. Everyone will do the same thing. So I don't need the virtual keyword.. So I can iterate through the list in x ms.. My question is, how much slower it would become if I just added that 'virtual' keyword?


If the body of the virtual function is more than a few lines (and since it's an Update method, I assume it will be), the speed difference will be negligible.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!