Advertisement
Guest User

array_merge() and loop and string2array tests

a guest
Jul 5th, 2014
264
0
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. function arr2str2arr($array, $rCount)
  23. {
  24.     return explode(',', ltrim(str_repeat(','. implode(',', $array), $rCount), ','));;
  25. }
  26.  
  27. set_time_limit(0);
  28.  
  29. $measure    = new \Benchmark\Measure;
  30. $array      = range(1, 1000);
  31. $count      = 1000;
  32.  
  33.  
  34. $x          = $measure->benchmarkTime('repeatMerge', [$array, $count], (int)1E2);
  35. $y          = $measure->benchmarkTime('repeatLoops', [$array, $count], (int)1E2);
  36. $z          = $measure->benchmarkTime('arr2str2arr', [$array, $count], (int)1E2);
  37. var_dump($x, $y, $z);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement