继承与实例化 [英] Inheritance Vs Instantiation

查看:123
本文介绍了继承与实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任何人都可以告诉我上述哪种方法(继承或实例化)在性能上更快
当我同时有两个选择要做时


问候
Chaithu

Hi,

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


Regards
Chaithu

推荐答案

通常,您在继承和实例化之间可能没有选择(也许在继承和组成之间?).
无论如何,您都应该过多地担心继承性能,因为像这样的内置"机制很快,而导致缓慢的真正原因通常是不良的开发人员算法.
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.


比较,因为没有其他选择.可以在不继承的情况下开发代码,但是在这种情况下,它与OOP无关.至于实例化,没有代码就没有代码,因此您没有什么可比较的.

让我们考虑在使用继承还是不使用继承之间进行选择.继承本身根本不影响性能.唯一影响性能的想法是基于虚拟方法的呼叫调度.当然,如果没有覆盖,则永远不需要虚拟方法.由于它是间接的一种附加级别(虽然只是一个级别),所以它具有一些较小的性能成本.

我不会在意这笔费用.它大约等于额外调用的成本(与嵌入式代码相比),也就是说,不必担心太多.

—SA
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.

—SA


这篇关于继承与实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆