Function Call vs Virtual Function Call
__attribute__((noinline))
int Compute(int a, int b) {
return (a * 3 + b * 7) ^ (a - b);
}
for (auto i = 0u; i < arr.size(); ++i) {
sum = Compute(sum, arr[i]);
}
^ This is Faster?
struct Base {
virtual int Compute(int a, int b) = 0;
};
Base* impl = GetImpl(); // not known at compile time
for (auto i = 0u; i < arr.size(); ++i) {
sum = impl->Compute(sum, arr[i]);
}
^ This is Faster?
* The benchmark is run under AMD Ryzen 9.
* For the full benchmark code, please refer here.
* For illustration purposes only, see FAQ for more details.