| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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). It is OK for me to have a separate binary to handle those cases. But the only design I came up with is with preprocessor to "separate" virtual keyword in class definition class Base { #ifdefine NOPOLYMORPHISM void method(); #else virtual void method(); #endif } and the part of the program where the concrete type of the objects is defined should be also modified. Is there any better way to do that? |
|
#2
| |||
| |||
| 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öm |
|
#3
| |||
| |||
| Litvinov Sergey <slitvinov@gmail.com> 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). If speed is crucial, you should profile and find out where best to spend your efforts, this is unlikely to be one of those places. In any case: vector<Base> vec(1); // make the vector as big as the // number of objects you have vec[0].method(); // no virtual overhead. |
|
#4
| |||
| |||
| Daniel T. wrote: > Litvinov Sergey <slitvinov@gmail.com> 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). > > If speed is crucial, you should profile and find out where best to > spend your efforts, this is unlikely to be one of those places. > > In any case: > > vector<Base> vec(1); // make the vector as big as the > // number of objects you have > vec[0].method(); // no virtual overhead. Are you sure about "no virtual overhead"? The vector: perator[]returns a *reference*, not an object. With a reference a call to a virtual function *should* go through the mechanism resolving all the virtual function calls to the final overrider. It's not statically resolved to Base::method, if that's what you implied. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
#5
| |||
| |||
| On Sep 6, 7:09 pm, Erik Wikström <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. > 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/art...rticle2015.asp) does very similar thing but I cannot find how to adopt it to my case. In the article there are two different classes but in my case I have Class+Derived Class as one of the options. |
|
#6
| |||
| |||
| On Sep 6, 7:09 pm, Erik Wikström <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öm 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. |
|
#7
| |||
| |||
| 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). Make Base a class template and specialise method(). -- Ian Collins. |
|
#8
| |||
| |||
| "Victor Bazarov" <v.Abazarov@comAcast.net> wrote: > Daniel T. wrote: > > Litvinov Sergey <slitvinov@gmail.com> 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). > > > > If speed is crucial, you should profile and find out where best to > > spend your efforts, this is unlikely to be one of those places. > > > > In any case: > > > > vector<Base> vec(1); // make the vector as big as the > > // number of objects you have > > vec[0].method(); // no virtual overhead. > > Are you sure about "no virtual overhead"? The vector: perator[]> returns a *reference*, not an object. With a reference a call to > a virtual function *should* go through the mechanism resolving all > the virtual function calls to the final overrider. It's not > statically resolved to Base::method, if that's what you implied. Oops. I forgot about that. |
|
#9
| |||
| |||
| On Sep 7, 12:09*am, Litvinov Sergey <slitvi...@gmail.com> wrote: > 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). It is OK for me to have > a separate binary to handle those cases. But the only design I came up > with is > with preprocessor to "separate" virtual keyword in class definition > > class Base { > #ifdefine NOPOLYMORPHISM > * void > method(); > #else > * virtual void > method(); > #endif > > } > > and the part of the program where the concrete type of the objects is > defined should > be also modified. > > Is there any better way to do that? Perhaps something like: struct Base { virtual void virtual_method() { base_method(); } void base_method(); void method() { if (s_use_virtual_) virtual_method(); else base_method(); } static bool s_use_virtual_; }; And set s_use_virtual_ at runtime based on whether you've created any derived objects. It still has some run-time overhead, but I think you'll find it's pretty small compared to out-of-line function invocation. HTH, Tony |
|
#10
| |||
| |||
| 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). It is OK for me to have > a separate binary to handle those cases. But the only design I came up > with is > with preprocessor to "separate" virtual keyword in class definition > > class Base { > #ifdefine NOPOLYMORPHISM > void > method(); > #else > virtual void > method(); > #endif > } > > and the part of the program where the concrete type of the objects is > defined should > be also modified. > > Is there any better way to do that? if you want `if' at compiling time, try template meta-programming. it's safer and cooler than macro. cheers, Jim |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.