Performance of static methods vs. functions

前端 未结 7 797
盖世英雄少女心
盖世英雄少女心 2020-11-29 21:50

In PHP, (unlike what I originally thought) there is an overhead of calling static methods vs simple functions.

On a very simple bench, the overhead is over 30% of th

7条回答
  •  一个人的身影
    2020-11-29 22:52

    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.

提交回复
热议问题