Advertisement
Guest User

array_merge() and loop tests

a guest
Jul 5th, 2014
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.67 KB | None | 0 0
  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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement