0

Possible Duplicate:
Virtual functions and performance - C++

I'm trying to refactor my code, and everywhere people say that using virtual functions is a huuuuge nono performance-wise, why? and is there another way that I can inherit a class and redefine functions that are defined in the base class?

Community
  • 1
  • 1
JamesBlandford
  • 178
  • 1
  • 8

2 Answers2

3

A good reference article for this topic can be found here: http://coldattic.info/shvedsky/pro/blogs/a-foo-walks-into-a-bar/posts/3

For the lazy, I guess the answer is "maybe slower slightly"

Rob
  • 304
  • 1
  • 5
1

Virtual functions are called through a vtable, which is basically an array of function pointers. So, every time one is called, there's an extra array lookup. I'm not sure if I'd call this a huuuuge nono though, in general they should be pretty fast.

From Wikipedia:

A virtual call requires at least an extra indexed dereference, and sometimes a "fixup" addition, compared to a non-virtual call, which is simply a jump to a compiled-in pointer. Therefore, calling virtual functions is inherently slower than calling non-virtual functions. An experiment done in 1996 indicates that approximately 6–13% of execution time is spent simply dispatching to the correct function, though the overhead can be as high as 50%.[4] The cost of virtual functions may not be so high on modern CPU architectures due to much larger caches and better branch prediction.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326