简体   繁体   English

PHP抽象类vs接口(性能)

[英]PHP abstract class vs interface (performance)

In PHP 5 and later what's faster - an interface or abstract class ? 在PHP 5和更高版本中, interfaceabstract class的速度更快?

abstract class MyPluginModel {

vs

interface MyPluginModel {

Any ideas how they compare in terms of memory and time performance? 有什么想法在内存和时间性能方面进行比较吗?

It doesn't make sense to compare the two based solely on performance, as they're not the same thing (the "interface vs. abstract class" question has been posted in this site several times – see eg here and here –, so I'm not going to reiterate the argument). 仅基于性能来比较这两者是没有意义的,因为它们不是同一件事(“接口与抽象类”问题已在此站点上发布多次,请参见此处此处 ,例如,因此我将不重申该论点)。

That said, an interface is very likely going to be faster since there are less things to be done (eg it's not necessary to copy the instance properties from the superclass to the subclass). 也就是说,由于要做的事情更少(例如,没有必要将实例属性从超类复制到子类),所以接口很可能会更快。 In the real world, the difference is, however, very unlikely to be noticed (even less than noticed if you're using an opcode cache, which you should, in production). 但是,在现实世界中,这种差异几乎不可能被注意到(甚至比在生产中应该使用的操作码缓存的情况要小得多)。

For further information, compare: zend_do_implement_interface , zend_do_inheritance . 有关更多信息,请比较: zend_do_implement_interfacezend_do_inheritance

As to memory, there shouldn't be any significant difference, both interfaces and abstract classes use the same data structure . 至于内存,应该没有太大的区别,接口和抽象类都使用相同的数据结构

The famous quote, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil", by Donald Knuth. 唐纳德·克努斯(Donald Knuth)的名言:“我们应该忘记效率低下,大约97%的时间:过早的优化是万恶之源”。

I really think you are doing premature optimization when you think about that. 我真的认为您在考虑时就在进行过早的优化。

interface and abstract class are not comparable from performance point of view. 从性能的角度来看,接口和抽象类是不可比的。 It is indeed impossible to say how much memory an abstract or interface type will hold since they both only hold references of any concrete object that inherits them down the inheritance hierarchy. 确实不能说抽象或接口类型将保留多少内存,因为它们都只保留对继承层次结构继承的任何具体对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM