Advertisement
Knyri

unset($v) vs $v=null

Jul 19th, 2013
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <pre>
  2. <?php
  3. echo '<h1>unset(..) vs ..=null with later assignment</h1>';
  4. define('TEST_SIZE',500000);
  5. $array = array_fill(0,TEST_SIZE,'TEST DATA');
  6. $start=microtime(true);
  7. for($i=0;$i<TEST_SIZE;$i+=2){
  8.     unset($array[$i]);
  9. }
  10. //comment out to just test the speed of unset
  11. for($i=0;$i<TEST_SIZE;$i++){
  12.     $dummy=isset($array[$i])?$array[$i]:null;
  13. }
  14. $unset=round(microtime(true)-$start,3);
  15. $array = array_fill(0,TEST_SIZE,'TEST DATA');
  16. $start=microtime(true);
  17. for($i=0;$i<TEST_SIZE;$i+=2){
  18.     $array[$i]=null;
  19. }
  20. //comment out to just test the speed of setting to null
  21. for($i=0;$i<TEST_SIZE;$i++){
  22.     $dummy=$array[$i];
  23. }
  24. $nullset=round(microtime(true) - $start,3);
  25.  
  26. $ttime=$unset+$nullset;
  27. echo "With an array; half unset or set to null.\n";
  28. echo "unset(..):      $unset ".round(100*$unset/$ttime,3)."%\n";
  29. echo "..=null:        $nullset ".round(100*$nullset/$ttime,3)."%\n";
  30.  
  31. $start = microtime(true);
  32. for($i=0; $i<TEST_SIZE; $i++){
  33.     //simple reassignment
  34.     $a= 'a';
  35.     $a= null;
  36. }
  37. $nullset= round(microtime(true) - $start,3);
  38.  
  39. $start= microtime(true);
  40. for ($i= 0; $i<TEST_SIZE; $i++){
  41.     //symbol is destroyed and recreated
  42.     $a= 'a';
  43.     unset($a);
  44. }
  45. $unset = round(microtime(true) - $start,3);
  46. $ttime=$unset+$nullset;
  47. echo "With a symbol.\n";
  48. echo "unset(..):      $unset ".round(100*$unset/$ttime,3)."%\n";
  49. echo "..=null:        $nullset ".round(100*$nullset/$ttime,3)."%\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement