You can use boilerplate logic parameters to eliminate runtime fork (excluding assembly if dead code is deleted).
template <bool computeMaxNorm = false> bool CheckConvergence() { if (computeMaxNorm) this->residual_max_norm = 0; for (size_t i = 0, I = this->X.Count(); i < I; ++i) { double abs_res = abs(this->F_X[i]); if (abs_res > this->convergenceCriterion) { this->isConverged = false; if (!computeMaxNorm) return false; } if (computeMaxNorm) { if (abs_res > this->residual_max_norm) this->residual_max_norm = abs_res; } } return this->isConverged = true; }
problem.CheckConverge<false>() will be faster than problem.CheckConverge<true>() , and this function will not cost without a break in runtime.
However, the processor branch predictor is generally very good, and branching at compile time may not matter.
source share