Performance of static methods vs. functions

前端 未结 7 798
盖世英雄少女心
盖世英雄少女心 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:36

    There is some thing Wrong in your tests. With a website designed to work with multiple users at the same time you have to create an object for each one. To run that object's method in your tests you should have:

    for($i=0; $i<10*1000*1000; $i++)
    { 
       $someObj = new someObj();
       $someObj->doTest($i); 
    }
    

    If your object had more properties and methods then creating it is slower and PHP uses more memory. A static method won't have this problem, and therefore using static methods is a better choice in lots of situations. For example, a class with some handy tools with static methods for common tasks.

提交回复
热议问题