Click here to Skip to main content
15,039,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

Can anyone please tell me which above methods(Inheritance or instantiation) is faster in performance
when i have both options to do

Sergey Alexandrovich Kryukov 20-May-11 21:23pm
You don't have those options! Wrong question.

Usually you don't have the choiche between inheritance and instantiation (maybe possibly between inheritance and composition?).
In any case you should worry too much about inheritance performance, because, 'builtin' mechanism like that are fast and the true causes of slowness are usually bad developer algorithms.
Sergey Alexandrovich Kryukov 20-May-11 21:33pm
I voted only 4 this time. Let me explain. This is a strange phrase"Usually you don't have the choiche...". What do you mean "usually"? This is never a choice!

Also, everyone should understand that inheritance itself does not incur any cost at all. This is purely compile-time stuff. Only when "virtual" is used it costs a bit. However, your practical advice is correct.

Please see my answer where I explain all that.
There is not comparison because there is not alternative. It is possible to develop code without inheritance, but it would nothing to do with OOP in this case. As to instantiation, there is not code without it, so you have nothing to compare.

Let's consider a choice between using inheritance or not using it. Inheritance itself does not effect performance at all. The only think affecting performance is call dispatching based on virtual method. Of course, you never need virtual method if there is not override. As it is an extra level of indirection (just one though), it has some minor performance cost.

I would not care about this cost. It is approximately equal to the cost of extra call (compared to embedded code), that is, it is not a cost to be worry too much.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900