# 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.

## 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 on other sites
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 on other sites
Quote:
 Original post by SiCraneVirtual 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 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 on other sites
Quote:
 Original post by DvDmanDTWell, 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.

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633337
• Total Posts
3011415
• ### Who's Online (See full list)

There are no registered users currently online

×