PHP 的 __get/__set/__call 性能问题
我有一个定制的 MVC PHP 框架,我正在重写它,并且有一个关于性能和魔术方法的问题。对于框架的模型部分,我在想 __get
/__set
魔术方法是否会导致过多的性能损失而不值得使用。我的意思是访问(读取和写入)模型数据将是最常见的操作之一。对于 MVC 框架的模型部分等大量使用的功能来说,使用 __get
/__set
魔术方法是否会对性能造成太大影响?
I have a custom-built MVC PHP framework that I am in the process of rewriting and had a question about performance and magic methods. With the model portion of the framework, I was thinking if __get
/__set
magic methods would cause too much performance hit to be worth using. I mean accessing (reads and writes) model data is going to be one of the most common things performed. Is the use of __get
/__set
magic methods too big of a performance hit for heavy use functionality like the model portion of a MVC framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
测量一下。
它肯定会对性能造成很大影响,特别是考虑到 PHP 中函数调用的成本很高。在下一版本的 PHP 中,这种差异将会更大,它实现了优化,可以显着更快地对声明的实例属性进行常规访问。
也就是说,PHP 很少是瓶颈。 I/O 和数据库查询通常需要更多时间。不过,这取决于您的使用情况;确定其是否符合基准测试的唯一方法。
这也是魔术方法的其他可读性问题。它们经常退化为一个大的
switch
语句并损害代码完成,这两者都可能会影响编程效率。Measure it.
It certainly has a big performance hit, especially considering function calls are expensive in PHP. This difference will be even bigger in the next version of PHP, which implements optimizations that render regular access to declared instance properties significantly faster.
That said, PHP rarely is the bottleneck. I/O and database queries frequently take much more time. This, however, depends on your usage; the only way to know for sure it to benchmark.
That are also other readability problems with the magic methods. They frequently degenerate into one big
switch
statement and compromise code completion, both of which may be hits in programming productivity.这是一篇一篇文章(三年前),其中包含一些基准。运行您自己的测试以查看它如何影响您的代码。
一般来说,它们要慢得多。但它们是瓶颈吗?这取决于你在做什么。如果您关心速度,那么它在哪里停止?
foreach
循环比for
循环慢,但大多数人不会重写所有代码以使用for
。仅使用 PHP 意味着代码的速度并不是那么重要。就我个人而言,我会喜欢任何能让编程变得更容易的风格。
Here's a an article (three years old) with some benchmarks. Run your own tests to see how it impacts your code.
Generally speaking, they are much slower. But are they the bottleneck? It depends what you are doing. If you're concerned about speed, where does it stop? The
foreach
loop is slower than thefor
loop, yet most people don't rewrite all of their code to use thefor
.Just using PHP means that the speed of your code isn't all that critical. Personally, I would favor whatever style makes it easier to program.
纯粹根据我的经验,它确实增加了很多开销。在大约有 4000 个 __get 的页面上(是的,该页面上有相当多的数据)性能明显变慢,以至于变得不可接受。我扔掉了所有
__set
的 &__get
用于不需要更改其他数据或检查外部依赖项(如外键)的变量,之后生成该页面的时间约为其生成时间的 15%之前拍的。Purely from my experience, it does add a quite a lot over overhead. On a page with about 4000
__get
's (yes, there was quite a lot of data on that page) performance was quite measurably slower, to the point it became unacceptable. I threw away all__set
's &__get
's for variables which wouldn't require other data to be altered or external dependancies (like foreign keys) to be checked, after which the time to generate that page was about 15% of the time it took before.我刚刚问了自己同样的问题并得出了同样的结论:最好以传统方式设置属性。 __get() + 大量开关会减慢一切
I have just asked myself this same question and came to the same conclusion: It's better to set the properties in the traditional way. __get() + massive switch slows everything down