SHARE
TWEET

array_merge() and loop tests

a guest Jul 5th, 2014 225 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require_once(__DIR__.'/autoload.php');
  2.  
  3. function repeatMerge($array, $rCount)
  4. {
  5.     return call_user_func_array('array_merge', array_fill(1, $rCount, $array));
  6. }
  7.  
  8. function repeatLoops($array, $rCount)
  9. {
  10.     $result = [];
  11.         $eCount = count($array);
  12.         for($j=0; $j<$rCount; $j++)
  13.         {
  14.                 for($i=0; $i<$eCount; $i++)
  15.                 {
  16.                    $result[]=$array[$i];
  17.                 }
  18.         }
  19.         return $result;
  20. }
  21.  
  22. set_time_limit(0);
  23.  
  24. $measure    = new \Benchmark\Measure;
  25. $array      = range(1, 1000);
  26. $count      = 1000;
  27.  
  28.  
  29. $x          = $measure->benchmarkTime('repeatMerge', [$array, $count], (int)1E2);
  30. $y          = $measure->benchmarkTime('repeatLoops', [$array, $count], (int)1E2);
  31. var_dump($x, $y);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top