For the below code example, the speed to execute method "increment" by the pointer - num_ptr is significantly slower than the local variable - num. I think it is related to the virtual method, but I don't understand why. Please help explain this. I am trying to understand the performance issue from this example.
#include <iostream>
const long long iterations_count = 1000000;
// a number interface
struct number {
virtual void increment() = 0;
};
struct concrete_number:number
{
long long a;
concrete_number(long long p){
a = p;
}
void increment()
{
a+=1;
}
};
int main() {
concrete_number num(0);
concrete_number* num_ptr = #
for (long long i = 0; i < iterations_count; i++) {
num.increment();
}
for (long long i = 0; i < iterations_count; i++) {
num_ptr->increment();
}
std::getchar();
}