0

Reading about virtual functions, I have come across the following concept: "C++ non virtual function calls are resolved at compile time with static binding, while virtual function calls are resolved at runtime with dynamic binding."

My questions are: 1)Would it have a large effect on performance then, if we had a large number of virtual function calls in our program, since they are evaluated at runtime? Or is it not that relevant in modern machines?

2)Is that number dependent on the stack?

Iason
  • 209
  • 3
  • 11
  • 38
  • Technically its slower for there is at least one or two more CPU ops before effectively calling the function, but the difference its so microscopic its not slower enough to matter, or even to be measured. You shouldn't worry about that. – Havenard Jul 10 '15 at 21:43
  • Thank you for the heads up. – Iason Jul 10 '15 at 21:52

2 Answers2

1

Is it slower? Most definitely. Is it enough to matter? Not really. Most modern OO languages have dynamic dispatch by default, which means all functions are virtual, and they're still moderately fast. Use it liberally but not unnecessarily.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
1

Generally speaking, the cost of referencing a vtable to get the function address for an object will be negligible.

But, like anything else, it depends on your application. A good rule of thumb is that if you are designing a class that you know will be speed-critical, then think about this type of issue. Otherwise, worry about bigger things.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
dan-O
  • 354
  • 1
  • 10