Archived

This topic is now archived and is closed to further replies.

mbenic

Global functions vs overloaded member functions?

Recommended Posts

Any profiling gurus out there have any idea about how much of a speed knock one takes as a result of using overloaded member functions instead of global functions. To put this in context, I want to design a decent particle management system, and class heirarchies with overloaded member functions seems far better in terms of clean design.. but the speed? And we''re not talking the occasional function call, it would be thousands+ times per sec. Any knowledge on this would be greatly appreciated.. including links to good articles on the subject (the speed issue, not designing particle systems). Thanks --- Matt Benic "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."

Share this post


Link to post
Share on other sites
Overloaded functions are no different from normal functions except that their name gets mangled differently (Fish may become Fish@Int_Double, for example, instead of staying Fish). Member functions are just like normal functions also, except that they get an extra hidden parameter called ''this''. If you make a member function static, it no longer gets passed this, so it''s the same speed as a global function.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
But in the case of non-static members is this still true? If so that''s great! (In this case I cant always use static member functions as I will need access to specific instance data). Thanks

---
Matt Benic

"Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning."

Share this post


Link to post
Share on other sites
Null and Void is basicly correct. The only thing I would add is that, while both a member function (with an implicit this parameter) and a global function (with an explicit instance parameter) have the same number of parameters, there is one SLIGHT difference. By default, the global function would use the cdecl calling convention, meaning that the instance parameter would be passed on the stack. However, the thiscall calling convention (used for member functions) passes "this" in a register (ecx I believe), which is a bit faster than a stack.

99% of the time, its not gonna make that much difference, but if you have a function you plan to call VERY VERY VERY often, then you might begin to notice a difference. For the most part, however, I think this is nitpicking, and you will only end up seeing a difference in a synthetic benchmark.

Edited by - LordKronos on January 11, 2002 8:05:12 AM

Share this post


Link to post
Share on other sites
Even if you are sitting with the following type of situation? :

class BaseParticle
{
private:
//lotsa members
public:
void DoSomething();
};

class ChildParticle : BaseParticle
{
private:
//more members
public:
void DoMore();
}

//After method details being filled in, the classes are used
BaseParticle *particle;
//particle ends up being a ChildParticle
particle->DoSomething();

I forget the correct terminology for calls being passed back up the heirarchy, but that''s the kind of thing Im worried about.

---
Matt Benic

"Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning."

Share this post


Link to post
Share on other sites
The only hit you take with the use of a virtual function is a lookup into the classes VTable to retrieve the address of the destination function (in late binding cases). This used to be a fairly significant hit, but this is one area where cache memory really comes to the rescue. While a explicit function call is generally faster, it is not by much... and I would consider to be pratically a wash.

Nick Pleis
"Game Programming for the Palm OS" Available February 2002

Share this post


Link to post
Share on other sites
No, there are no problems there (performance or otherwise). The object will simply be passed to the method with no fuss.

Now, on the other hand, if you made the method virtual, that would be a SLIGHTLY different story (though not much). In the case of virtual methods, at run time code would be executed to look in the VTable for particle and find the address for the method. Thats only one additional step. Instead of:
push params
jump to known address

it becomes :
push params
get method address from memory location ''particle + known_offset''
jump to address we just looked up

Very minimal difference (again, you would likely only notice in synthetic benchmarks). However, when you get to dealing with virtual methods you are comparing apples to oranges since virtual methods serve a completely different purpose which global methods could never replace.

Share this post


Link to post
Share on other sites
Thanks a ton to each of you. Much appreciated :D

---
Matt Benic

"Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning."

Share this post


Link to post
Share on other sites