Guest User

Untitled

a guest
Jun 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. $a1 = array('foo');
  2. $a2 = $a1;
  3. $a2[0] = 'bar';
  4. // now $a1[0] is foo, and $a2[0] is bar. The array is copied
  5.  
  6. $a1 = array('foo');
  7. $a2 = $a1; // <-- this should make a copy
  8. // but $a1 and $a2 point to the same data internally
  9. $a2[0] = 'bar';
  10. // now $a1[0] is foo, and $a2[0] is bar. The array is really copied
  11.  
  12. <?php
  13.  
  14. ini_set('memory_limit', '64M');
  15.  
  16. function ttime($m) {
  17. global $s;
  18. echo $m.': '.(microtime(true) - $s).'<br/>';
  19. $s = microtime(true);
  20. }
  21.  
  22. function aa($a) {
  23. return $a;
  24. }
  25.  
  26. $s = microtime(true);
  27. for ($i = 0; $i < 200000; $i++) {
  28. $array[] = $i;
  29. }
  30. ttime('Create');
  31. $array2 = aa($array); // or $array2 = $array
  32. ttime('Copy');
  33. $array2[1238] = 'test';
  34. ttime('Modify');
  35.  
  36. Create: 0.0956180095673
  37. Copy: 7.15255737305E-6
  38. Modify: 0.0480329990387
Add Comment
Please, Sign In to add comment