Re: How make polymorphism optional?
On Sep 6, 7:09 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-09-06 17:09, Litvinov Sergey wrote:
My problem is the following one.
I have a huge number of objects of base class:
class Base {
public:
virtual void
method();
}
And a derived class:
class Derived : publcic Base {
public:
virtual void
method();
}
Sometime I have no objects of Derived class and in those cases
I would like to get rid of polymorphism overhead. (speed is crucial
for
me).
I have to ask, have you measured and made sure that it is the
polymorphism that is your performance problem? If you have not carefully
profiled your program yet you should do so before you consider how to
speed it up.
If you do not use the Derived class I think the easiest way would be to
simply #ifdef out the declaration of it, the compiler should then be
able to optimise away the polymorphism. Of course, there is also a
chance that it already does so whenever it can.
--
Erik Wikstr=F6m
Thank you for the comment. I have currently two versions and the
profile (compiler: gcc 4.2.3, profiler: gprof) shows the difference
worth the effort.
Also I found that "compile time polymorphism" (http://www.gamedev.net/
reference/articles/article2015.asp)
does very similar thing but I cannot find how to adopt it to my case.
The example in the
articles deals with two different classes but I have Class+Derived
Class as one
of the options.