Stijn Oude Brunink said:
Hello,
I have the following trade off to make:
A base class with 2 virtual functions would be realy helpfull for the
problem I'm working on. Still though the functions that my program will use
a lot are the ones that are virtual and thus will slow down the
calculation, at least that is what what I have read on several places on the
internet. It makes sense the program has to work with some kind off lookup
table for the virtual functions to excatly know what function to call.
How much is the slow down and is there a workaround?
I have been thinking about using function pointers but that seems as nasty
trick which will only create a lot development problems later on.
thanks for the respons
stijn
I have some comments to make that may help:
1) Don't worry about the speed of virtual functions until you know for
a fact that it is a problem. Also, don't trust everything you read on
the Internet. It boils down to this: virtual functions are relatively
slow. This has some very important implications. First, virtual
functions are NOT slow. Second, depending on the application in
question, the relative difference MAY NOT be significant. How can you
tell if the relative difference is significant? By using a code
profiler. Let's assume that you are not meeting your time
requirements. Run the profiler and let the profiler tell you what are
the bottlenecks. Chances are you might be surprised that it indicates
something else than what you thought.
2) OK, let's assume that for whatever reason you don't want to use
virtual functions. The only solution that I know of, with respect to
speed, is to use templates. Templates have the ability to take
polymorphism from the run-time level to the compile-time level. It
means that the dynamic mechanism happens during compilation. The end
result is that the function call becomes a non-virtual function call.
However, this is easier said then done. I have mixed feelings about
template programming. It can be very tricky. Also, depending on how
much transparency your client code needs it may not be possible to use
the compile-time polymorphic technique of templates. If you are still
interested in using templates then lookup and read about the
"Curiously Recursive Template" pattern.
I'd say don't resort to templates unless you absolutely have to. In
my experience run-time polymorphism is not the performance evil that
many make it out to be. I currently work on a project that involves
live video feed. The code base is very object-oriented. It has many
strategy design patterns. Simply stated, I have two main tasks I want
to accomplish: grabbing a frame and preprocessing each frame. Both of
these tasks have nothing to do with the main application logic. If I
want to use different cameras then I have to be able switch the frame
grabbing and preprocessing strategy as seamlessly as possible. During
live capture my main application logic is calling virtual functions
from an abstract interface. Does the overhead of the virtual
functions slow down the frame rate? No. But then again it depends on
context. If the frame rate is more than 100 frames per second (fps)
then maybe the virtual functions will hurt performance. However,
fewer than 32 fps and the virtual functions are negligible. I'm not
saying this is true for all applications of this type. I'm saying
that it is true for my application. The profiler tells me that my
bottleneck, by far, is the preprocessor. Specifically, it is the
convolution operation (a standard image processing technique).
Calling the preprocessor through a function or virtual function would
make no difference in the total execution time for the convolution
operation. What did we have to do to speed up the live capture? We
used MMX instructions for the pixel processing inside the
preprocessor. No need to remove the virtual functions. They are
still there and my code base remains simple and maintainable. Just
something to think about.
Nicholas