PHP 中单例类实例方法与静态类方法的性能比较?
我感兴趣的是客观分析哪个性能更好;调用单例类的实例方法或静态类的方法。我已经看过这个所以我我并不是在寻找关于两者之间差异的讨论,或者讨论哪个“更好”。我只对两者之间的相对表现感兴趣。提前致谢。
-麦克风
I'm interested in objective analysis of which is more performant; calling instance methods of a singleton class or methods of a static class. I've already seen this so I'm not looking for a discussion about the difference between the two or a discussion of which is "better." I'm only interested in relative performance between the two. Thanks in advance.
-Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查此图表:)
从 这篇文章
Check this chart :)
grabbed from this article
除非您在紧密循环中调用它们(意味着没有其他重要的代码,其中调用的开销很大)数千或数十万次,否则不必担心。差异可能会在一微秒以内,因此不值得担心。只需做出最好的架构选择...
过早的优化是万恶之源...
编辑: 对于所有反对者,这里是 我写的一篇博客文章,描述了为什么性能比较,例如这些都是无用的。
Unless you're calling them in a tight loop (meaning no other significant code, where the overhead of the call is significant) thousands or hundreds of thousands of times, don't worry about it. The difference is likely going to be under a microsecond, so it's not worth fretting over. Simply make the best architectural choice...
Premature optimization is the root of all evil...
Edit: To all the downvoters, here's a blog post that I wrote that describes why performance comparisons like this are all but useless.
在调用单例模式对象的实例方法之前,您需要先获取实例,这需要静态方法调用:
因此,第一次需要在函数中访问该对象时,需要进行静态方法调用第一的。因为函数调用从来都不是免费的,所以最好将方法设置为静态。
Before you can call the instance method of a singleton pattern object, you need to get the instance first, which requires a static method call:
So the first time you need access to that object in a function, you need to make a static method call first. Because function calls are never free, you are probably better off making the method static.
在我之前所做的测试中,我发现调用静态方法比调用实例方法更快,并且内存效率更高......但单例不应该纯粹因为这些原因而被忽略。
In previous tests that I've done, I've found that calling static methods is faster than calling instance methods, and fractionally more memory efficient.... but the singleton shouldn't be dismissed purely for those reasons.
我这次谈话有点晚了,但刚刚发现这个问题,我想我会把我的想法投入到我的第一篇帖子中。
作为一个快速实验(在阅读 zolex 链接的文章后),我在文章的基准测试中添加了第三个测试用例:
当然,结果并不是 100% 一致,但我发现大多数时候通过所有三种方法运行 500,000 次调用时,上述方法的运行速度比其他两种方法快 2-3 秒。
尽管当我看到给出的“过早优化”引用时我总是感到畏缩,但在这种情况下,我认为它击中了要害。性能差异充其量是最小的,并且通常有利于更合理的单例使用。
I'm a bit late to this conversation, but having just found the question I figured I would toss my thoughts into the ring for my first post to SO.
As a quick experiment (after reading the article linked by zolex) I added a third test case to the article's benchmarks:
The results weren't 100% consistent of course, but I found that most times when running 500,000 calls through all three methods, the above method was running anywhere from 2-3 seconds faster than either of the other two.
Though I always cringe when I see the 'premature optimization' quote given, in this case I think it hits the nail on the head. The performance difference is minimal at best, and is usually in favor of the more sane singleton usage.