This question is more or less similar to this Extend an existing API: Use default argument or wrapper function?
but sufficiently different.
Please read.
I have a class Foo that has function oldFunc that is used in many parts of my program.
In order to introduce new behaviour to Foo I implemented newFunc and made it private so that
callers to oldFunc keep using the same API but can get the new behaviour if they set a flag by
calling setUseType() function.
Pseudo code follows:
class Foo
{
public:
Foo(){ useNew = false;}
void setUseType(bool useType){ useNew = useType;}
std::string oldFunc(int r)
{
if(useNew )
{
return newFunc(r);
}
return std::string("Some value after doing some work");
}
private:
std::string newFunc(int r){ return std::string("Some value after doing some work");}
bool useNew;
};
Question oldFunc is called many times ( millions of times) inside a loop.
How much overhead should I expect from wrapping newFunc inside oldFunc.
Theoretically if oldFunc and newFunc where indentical in implementation is there a reason to expect
calls with a flag setUseType(true) to be slower?
In this particular case, there will be no overhead on any non-idiotic compiler as the function will be inlined. Your code would be equivalent to:
If it were not inlined, there would be an extra jump for the call to the new function. That’s where all the extra computation time goes into. This extra jump will most likely cause a lot less delay, even considering millions of calls. I suggest you do some profiling if you really do have speed issues and figure out the real bottleneck – most likely not the extra call.