Which is faster ?
A. Call 1000 times a virtual function OR
B. call 1000 times a normal function trough a function pointer.

For example:

A.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Base
{
public: virtual void myfunc() = 0;
};

class Child: public Base
{
public: void myfunc(){...}; //overriding the base class function
}

int main()
{
Child myclass;

call myclass.myfunc() 1000 times
}



B.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class SomeClass
{
public: void myfunc(){ ... }
};

int main()
{
SomeClass myclass;

void (*foo)();
foo = &myclass.myfunc;

call foo() 1000 times
}


Sponsored Links

What do You guys think which one would perform better ?
Assume that the compiler will surely not be able decide the class type in the virtual function case (A). And also the functions are quite complex and big.