What's better at freeing memory with PHP: unset() or $var = null

前端 未结 13 2167
陌清茗
陌清茗 2020-11-22 08:57

I realise the second one avoids the overhead of a function call (update, is actually a language construct), but it would be interesting to know if one is be

13条回答
  •  萌比男神i
    2020-11-22 09:53

    Code example from comment

    echo "PHP Version: " . phpversion() . PHP_EOL . PHP_EOL;
    
    $start = microtime(true);
    for ($i = 0; $i < 10000000; $i++) {
        $a = 'a';
        $a = NULL;
    }
    $elapsed = microtime(true) - $start;
    
    echo "took $elapsed seconds" . PHP_EOL;
    
    
    
    $start = microtime(true);
    for ($i = 0; $i < 10000000; $i++) {
        $a = 'a';
        unset($a);
    }
    $elapsed = microtime(true) - $start;
    
    echo "took $elapsed seconds" . PHP_EOL;
    

    Running in docker container from image php:7.4-fpm and others..

    PHP Version: 7.4.8
    took 0.22569918632507 seconds null
    took 0.11705803871155 seconds unset
    took 0.20791196823121 seconds null
    took 0.11697316169739 seconds unset
    
    PHP Version: 7.3.20
    took 0.22086310386658 seconds null
    took 0.11882591247559 seconds unset
    took 0.21383500099182 seconds null
    took 0.11916995048523 seconds unset
    
    PHP Version: 7.2.32
    took 0.24728178977966 seconds null
    took 0.12719893455505 seconds unset
    took 0.23839902877808 seconds null
    took 0.12744522094727 seconds unset
    
    PHP Version: 7.1.33
    took 0.51380109786987 seconds null
    took 0.50135898590088 seconds unset
    took 0.50358104705811 seconds null
    took 0.50115609169006 seconds unset
    
    PHP Version: 7.0.33
    took 0.50918698310852 seconds null
    took 0.50490307807922 seconds unset
    took 0.50227618217468 seconds null
    took 0.50514912605286 seconds unset
    
    PHP Version: 5.6.40
    took 1.0063569545746 seconds null
    took 1.6303179264069 seconds unset
    took 1.0689589977264 seconds null
    took 1.6382601261139 seconds unset
    
    PHP Version: 5.4.45
    took 1.0791940689087 seconds null
    took 1.6308979988098 seconds unset
    took 1.0029168128967 seconds null
    took 1.6320278644562 seconds unset
    
    

    But, with other example:

    Results:

    PHP Version: 7.4.8
    took 0.053696155548096 seconds
    took 0.053897857666016 seconds
    
    PHP Version: 7.3.20
    took 0.054572820663452 seconds
    took 0.054342031478882 seconds
    
    PHP Version: 7.2.32
    took 0.05678391456604 seconds
    took 0.057311058044434 seconds
    
    
    PHP Version: 7.1.33
    took 0.097366094589233 seconds
    took 0.073100090026855 seconds
    
    PHP Version: 7.0.33
    took 0.076443910598755 seconds
    took 0.077098846435547 seconds
    
    PHP Version: 7.0.33
    took 0.075634002685547 seconds
    took 0.075317859649658 seconds
    
    PHP Version: 5.6.40
    took 0.29681086540222 seconds
    took 0.28199100494385 seconds
    
    PHP Version: 5.4.45
    took 0.30513095855713 seconds
    took 0.29265689849854 seconds
    
    

提交回复
热议问题