Some context:
I have a C++ method that performs some intensive computation (some branch-and-bound algorithm on a Vehicle Routing Problem variation). Efficiency is therefore paramount in this code.
As I am testing different tricks to achieve optimal speed, I ended up implementing a class StatGatherer
that collects information during a given run of the algorithm (namely : how many feasible paths were found, how many were bounded, how many were found unfeasible...). The code looks like this:
void doStuff(const shared_ptr<StatGatherer>& statGatherer = NULL)
{
//do some stuff
...
if (statGatherer != NULL && some unfeasibility condition)
statGatherer->countOneFeasiblePath();
//do more stuff
...
if (statGatherer != NULL && some bounding criterium on the current path)
statGatherer->countOneBoundedPath();
//do more stuff
...
if (statGatherer != NULL && a whole path has been found)
statGatherer->countOneBoundedPath();
...
//...more information gathering triggered by certain events
}
This works well enough, but ironically, the presence of this kind of 'profiling' code involving statGatherer
slows down the algorithm quite a bit, as the above pseudo-code is executed tens of millions of times. Even when statGatherer
is not provided and defaulted to null, it is still quite a bit slower than not having this code at all.
My question is therefore the following : is there a design that would allow me to achieve the same behavior, but with no loss of efficiency when I do not require to gather statistics, compared to not having this code at all?
Every template solution I can think of still seems to involve some kind of run-time checks like above, so still more time-consuming.
Thanks a lot for your help!
PS: I'm new here so I welcome constructive feedback to make my questions clearer.