Virtual methods?

Started by
3 comments, last by foofightr 18 years, 7 months ago
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?
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.
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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?
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.

This topic is closed to new replies.

Advertisement