Is it possible that instead of checking a bool (and thus branchning) in a function, it could be faster to evaluate the bool beforehand and just change a function pointer to the desired behaviour?
Consider:
#include <iostream>
class A
{
public:
A()
{
workTrue = [] () { std::cout << "True" << std::endl; };
workFalse = [] () { std::cout << "False" << std::endl; };
SetArgs(false);
}
void SetArgs(bool _b)
{
b = _b;
work = b ? &workTrue : &workFalse;
}
void DoWork()
{
(*work)();
// b ? workTrue() : workFalse();
}
private:
std::function<void()> workTrue;
std::function<void()> workFalse;
std::function<void()>* work;
bool b;
};
int main()
{
A a;
a.DoWork();
}
Note: this is the same question as Pointer dereferencing overhead vs branching / conditional statements , only in a more abstract form. The question in this link was not answered satisfactory, in the sense that the core of the question (de-ref vs branch) was largely ignored.