PHP: performance of static methods vs functions?

It has been a while since I have done any PHP but this is probably similar to what you expect in other programming environments It is likely that the static method requires some construction of a SomeClass object behind the scenes each time that it is called, whereas the function can just be executed without any startup cost. Creating an object could be costly depending on a number of things: destruction of existing objects by a garbage collector/reference counter, memory pressure causing fragmentation, suboptimal memory allocation policies in the C runtime etc It would be interesting to compare the method performance of an existing object. To do this create an instance of SomeClass and then call an instance method repeatedly.

It has been a while since I have done any PHP but this is probably similar to what you expect in other programming environments. It is likely that the static method requires some construction of a SomeClass object behind the scenes each time that it is called, whereas the function can just be executed without any startup cost. Creating an object could be costly depending on a number of things: destruction of existing objects by a garbage collector/reference counter, memory pressure causing fragmentation, suboptimal memory allocation policies in the C runtime etc.It would be interesting to compare the method performance of an existing object.

To do this create an instance of SomeClass and then call an instance method repeatedly.

Updated question with object method benchmark - not really the result I would imagine. – jcinacio Sep 24 '09 at 16:43.

In the case of the static method, PHP has to check wether the method can or cannot be called from the calling context (public, protected, private). That's most likely what causes the overhead, or at least part of it, since the classic function call doesn't require PHP to perform that kind of check.

That makes sense - however, calling an object method is faster, and the same rules apply... – jcinacio Sep 24 '09 at 16:54 Maybe PHP checks wether a specific object method can be called from the current context or not, only once, and stores that information in memory as long as the execution loop remains in this same context...But does not do that for static methods. Man, you got me wondering why, now :) That a question you could ask on the PHP dev list! – Nicolas Sep 24 '09 at 17:05.

I repeated the test on my machine multiple times and surprisingly you are right! In PHP calling methods of static class seems to be slower than calling object methods. Click here for simple test.

The code with the running test is in the above link. I even tried placing both the objet method and the static method in the same class and the static method still results SLOWER! At this point I'm wondering how slow could be a call to a static method of an inherited class, since inheritance adds up delay.

Sadly, I'm clueless about the reason. Maybe PHP takes more time in finding the definition of the static method. As a side not I could only say that in a real life application it usually happens to have the object created before calling one of its methods.

Therefor your test should take this into account comparing the loop of static calls to a loop that each time (or at lest some times) * creates the objet: for ($i = 0; $idoTest($i); } thus is obviously slower than the static call. * the problem is: how much is some times in order to simulate what happnes in a real world app? It's hard to say!

Here is an article that discusses differences in performance between some of these concepts: webhostingtalk.com/showthread.php?t=538076.

Thank you for the link. However, results there are actually somewhat inconclusive so not really helpful. – jcinacio Sep 24 '09 at 16:23.

A framework or a cms has 5000- 10000 function calls per request.

You do not need to care about this. If you do, that means you have done something wrong designing your system. A performance should be attempted to be increased using other ways.

I do know optimization starts at the "cheapest" (easier) points, but I also cannot overlook the fact that performance differences exist between different ways of achieving the same result. Also, the values in cause - 20% - 30% overhead - are not to be taken lightly, specially for code that does these calls a very large number of times. – jcinacio Sep 25 '09 at 16:37 1 If you really care so much about performance, you should not use PHP at all.

Interpreting source code and even executing byte code is so slow... Pure C is the key! Your 20-30% are only several microseconds or milliseconds. Who cares how much user awaits server response - 20 or 40 milliseconds?

Your database queries and template fetching will eat up all your optimizations. You started to optimize in the wrong place. Read Fouler.

– FractalizeR Sep 28 '09 at 11:24 2 the op asked what happens behind the scenes in php...if he need to care about it or not is his decision. – JohnSmith Apr 20 at 21:01.

In PHP calling methods of static class seems to be slower than calling object methods. Click here for simple test. The code with the running test is in the above link.

I even tried placing both the objet method and the static method in the same class and the static method still results SLOWER! At this point I'm wondering how slow could be a call to a static method of an inherited class, since inheritance adds up delay. Sadly, I'm clueless about the reason.

Maybe PHP takes more time in finding the definition of the static method. As a side note I could only say that in a real life application it usually happens to have the object created before calling one of its methods. Thus is obviously slower than the static call.

* the problem is: how much is some times in order to simulate what happnes in a real world app? It's hard to say!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions