Performance of static methods vs. functions

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

    I am following up what Morgan Touverey Quilling did but with PHP 7. Did 3 iterations incase it takes longer for the first run vs subsequent runs. Includes all classes as it might be done realistically. All included files just return the input.

    include 'lib/global.php';
    include 'SomeClass.php';
    include 'StaticTest.php';
    
    $someObj = new SomeClass();
    
    $starttime = microtime(true);
    for ($i = 0; $i< 10*100000; $i++)
        StaticTest::doStaticTest($i);
    
    echo "
    Static Time: " , (microtime(true)-$starttime) , " ms\n"; // bench object method $starttime = microtime(true); for ($i = 0; $i< 10*100000; $i++) $someObj->doObjTest($i); echo "
    Object Time: " , (microtime(true)-$starttime) , " ms\n"; // bench function $starttime = microtime(true); for ($i = 0; $i< 10*100000; $i++) something_doTest($i); echo "
    Function Time: " , (microtime(true)-$starttime) , " ms\n"; echo "
    Static Time: " , (microtime(true)-$starttime) , " ms\n"; // bench object method $starttime = microtime(true); for ($i = 0; $i< 10*100000; $i++) $someObj->doObjTest($i); echo "
    Object Time: " , (microtime(true)-$starttime) , " ms\n"; // bench function $starttime = microtime(true); for ($i = 0; $i< 10*100000; $i++) something_doTest($i); echo "
    Function Time: " , (microtime(true)-$starttime) , " ms\n"; echo "
    Static Time: " , (microtime(true)-$starttime) , " ms\n"; // bench object method $starttime = microtime(true); for ($i = 0; $i< 10*100000; $i++) $someObj->doObjTest($i); echo "
    Object Time: " , (microtime(true)-$starttime) , " ms\n"; // bench function $starttime = microtime(true); for ($i = 0; $i< 10*100000; $i++) something_doTest($i); echo "
    Function Time: " , (microtime(true)-$starttime) , " ms\n";

    Just note that this is being done on one of my webhosts as its easier to switch php versions so might be some noise.

    PHP 7.0.33

    Static Time:   0.14076709747314 ms
    Object Time:   0.16203689575195 ms
    Function Time: 0.13194108009338 ms
    Static Time:   0.13194918632507 ms
    Object Time:   0.1779100894928 ms
    Function Time: 0.13044309616089 ms
    Static Time:   0.13045001029968 ms
    Object Time:   0.16074585914612 ms
    Function Time: 0.13029479980469 ms 
    

    PHP 7.1.29

    Static Time:   0.13407206535339 ms
    Object Time:   0.13267111778259 ms
    Function Time: 0.1302649974823 ms
    Static Time:   0.13027906417847 ms
    Object Time:   0.1390438079834 ms
    Function Time: 0.16873598098755 ms
    Static Time:   0.16874289512634 ms
    Object Time:   0.13901305198669 ms
    Function Time: 0.12576103210449 ms 
    

    PHP 7.2.18:

    Static Time:   0.1657600402832 ms
    Object Time:   0.15700101852417 ms
    Function Time: 0.1484169960022 ms
    Static Time:   0.14842295646667 ms
    Object Time:   0.16168689727783 ms
    Function Time: 0.17508292198181 ms
    Static Time:   0.17508983612061 ms
    Object Time:   0.19771790504456 ms
    Function Time: 0.1468551158905 ms 
    

    PHP 7.3.5

    Static Time:   0.10701704025269 ms
    Object Time:   0.097011089324951 ms
    Function Time: 0.075740098953247 ms
    Static Time:   0.07575798034668 ms
    Object Time:   0.083790063858032 ms
    Function Time: 0.072473049163818 ms
    Static Time:   0.072479009628296 ms
    Object Time:   0.081503868103027 ms
    Function Time: 0.071882963180542 ms 
    

    PHP 7.2 seemed to run a lot slower than the other versions on average. I found their lowest number but it got into the low .2####'s too. Don't' have 7.4 as of right now.

提交回复
热议问题