title: PHP Performance : Copy vs. Reference
body: <p>Hey there. Today I wrote a small benchmark script to compare performance of copying variables vs. creating references to them. I was expecting, that creating references to large arrays for example would be significantly slower than copying the whole array. Here is my benchmark code:</p>
  
  <pre><code>&lt;?php
      $array = array();
  
      for($i=0; $i&lt;100000; $i++) {
          $array[] = mt_rand();
      }
  
      function recursiveCopy($array, $count) {
          if($count === 1000)
              return;
  
          $foo = $array;
          recursiveCopy($array, $count+1);
      }
  
      function recursiveReference($array, $count) {
          if($count === 1000)
              return;
  
          $foo = &amp;$array;
          recursiveReference($array, $count+1);
      }
  
      $time = microtime(1);
      recursiveCopy($array, 0);
      $copyTime = (microtime(1) - $time);
      echo "Took " . $copyTime . "s \n";
  
  
      $time = microtime(1);
      recursiveReference($array, 0);
      $referenceTime = (microtime(1) - $time);
      echo "Took " . $referenceTime . "s \n";
  
      echo "Reference / Copy: " . ($referenceTime / $copyTime);
  </code></pre>
  
  <p>The actual result I got was, that recursiveReference took about 20 times (!) as long as recursiveCopy. </p>
  
  <p>Can somebody explain this PHP behaviour? </p>