the difference between unset and = null

后端 未结 5 1757
暖寄归人
暖寄归人 2020-11-29 04:03

From a random php.net post:

If you are doing $whatever = null; then you are rewriting variable\'s data. You might get memory freed / shrunk fast

5条回答
  •  感动是毒
    2020-11-29 04:17

    Using the code

    $a = str_repeat('hello world ', 10000);
    
    $start1 = microtime(true);
    unset($a);
    $stop1 = microtime(true);
    
    $a = str_repeat('hello world ', 10000);
    
    $start2 = microtime(true);
    $a = null;
    $stop2 = microtime(true);
    
    echo 'unset time lap of '. ( $stop1 - $start1 ) .'
    '; echo 'null time lap of '. ( $stop2 - $start2 ) .'
    ';

    for 10 times:

    unset time lap of 5.0067901611328E-6
    null time lap of 1.1920928955078E-6
    
    unset time lap of 9.5367431640625E-7
    null time lap of 9.5367431640625E-7
    
    unset time lap of 0
    null time lap of 9.5367431640625E-7
    
    unset time lap of 2.1457672119141E-6
    null time lap of 1.1920928955078E-6
    
    unset time lap of 2.1457672119141E-6
    null time lap of 0
    
    unset time lap of 9.5367431640625E-7
    null time lap of 0
    
    unset time lap of 1.9073486328125E-6
    null time lap of 9.5367431640625E-7
    
    unset time lap of 9.5367431640625E-7
    null time lap of 0
    
    unset time lap of 1.9073486328125E-6
    null time lap of 9.5367431640625E-7
    
    unset time lap of 0
    null time lap of 0
    

    Looks like null assignment has less processing time more often.

提交回复
热议问题