Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Benchmark for array_repeat

By: a guest on Aug 21st, 2013  |  syntax: PHP  |  size: 4.13 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. header('Content-type: text/plain');
  4.  
  5. $sum = microtime(true);
  6.  
  7. $start = microtime(true);
  8. for($i = 0; $i < 100000; $i++)
  9. {
  10.         // the variables
  11.         $array = array("a", "b", "c", "d");
  12.         $desiredLength = 71;
  13.         $newArray = array();
  14.         // create a new array with AT LEAST the desired number of elements by joining the array at the end of the new array
  15.         while(count($newArray) <= $desiredLength){
  16.             $newArray = array_merge($newArray, $array);
  17.         }
  18.         // reduce the new array to the desired length (as there might be too many elements in the new array
  19.         $array = array_slice($newArray, 0, $desiredLength);
  20. }
  21. $duration = microtime(true)-$start;
  22. echo 'Kau-Boy: ',$duration,"\n";
  23.  
  24. $start = microtime(true);
  25. for($i = 0; $i < 100000; $i++)
  26. {
  27.         $array = array('a', 'b', 'c', 'd');
  28.         $length = 71;
  29.         $result = array();
  30.         while(count($result) < $length)
  31.         {
  32.           $current = each($array);
  33.           if($current == false)
  34.           {
  35.             reset($array);
  36.             continue;
  37.           }
  38.           $result[] = $current[1];
  39.         }
  40. }
  41. $duration = microtime(true)-$start;
  42. echo 'Frxstrem: ',$duration,"\n";
  43.  
  44. function fillArray1($length, $values) {
  45.     foreach (new InfiniteIterator(new ArrayIterator($values)) as $element) {
  46.         if (!$length--) return $result;
  47.         $result[] = $element;
  48.     }
  49.     return $result;
  50. }
  51.  
  52. $start = microtime(true);
  53. for($i = 0; $i < 100000; $i++)
  54. {
  55.         $result = fillArray1(71, array('a', 'b', 'c', 'd'));
  56. }
  57. $duration = microtime(true)-$start;
  58. echo 'NikiC: ',$duration,"\n";
  59.  
  60. $start = microtime(true);
  61. for($i = 0; $i < 100000; $i++)
  62. {
  63.         $arr = array("a", "b", "c", "d");
  64.         $len = 71;
  65.         $a = array();
  66.         $a = str_split( substr( str_repeat( join( $arr), ceil( $len / count( $arr))), 0, $len));
  67. }
  68. $duration = microtime(true)-$start;
  69. echo 'user2469998: ',$duration,"\n";
  70.  
  71. $start = microtime(true);
  72. for($i = 0; $i < 100000; $i++)
  73. {
  74.         $array = array("a", "b", "c", "d");
  75.         $merge = array();
  76.         $desiredLength = 71;
  77.         while(2 * count($array) <= $desiredLength){
  78.         $array = array_merge($array, $array);
  79.         }
  80.         if($desiredLength > count($array))
  81.         $merge = array_slice($array, 0, $desiredLength - count($array));
  82.         $array = array_merge($array, $merge);
  83.         $array = array_slice($array, 0, $desiredLength);
  84. }
  85. $duration = microtime(true)-$start;
  86. echo 'Alexander: ',$duration,"\n";
  87.  
  88. $start = microtime(true);
  89. for($n = 0; $n < 100000; $n++)
  90. {
  91.         $newarray = array();
  92.         $i = 0;
  93.         $oldarrayvalues = array("a", "b", "c", "d");
  94.         $oldarraysize = count($oldarrayvalues);
  95.         if ( $oldarraysize ) {
  96.             while ( count($newarray) < 71 ) {
  97.                 $newarray[] = $oldarrayvalues[$i];
  98.                 $i++;
  99.                 $i %= $oldarraysize;
  100.             }
  101.         }
  102. }
  103. $duration = microtime(true)-$start;
  104. echo 'Hammerite: ',$duration,"\n";
  105.  
  106. function fill(array $initalArray, $toCount) {
  107.     $initialArrayCount = count($initalArray);
  108.  
  109.     $fillUp = function(array $filledUpArray, $missingCount)
  110.                     use(&$fillUp, $initalArray, $initialArrayCount, $toCount)
  111.     {
  112.         if($missingCount <= 0) return array_slice($filledUpArray, 0, $toCount);
  113.         return $fillUp(array_merge($filledUpArray, $initalArray), $missingCount - $initialArrayCount);
  114.     };
  115.  
  116.     return $fillUp($initalArray, $toCount - $initialArrayCount);
  117. }
  118.  
  119. $start = microtime(true);
  120. for($i = 0; $i < 100000; $i++)
  121. {
  122.         $filledArray = fill(array("a", "b", "c", "d"), 71);
  123. }
  124. $duration = microtime(true)-$start;
  125. echo 'Max: ',$duration,"\n";
  126.  
  127.  
  128. $start = microtime(true);
  129. for($i = 0; $i < 100000; $i++)
  130. {
  131.         $array = array('a', 'b', 'c', 'd');
  132.         $end   = 71;
  133.        
  134.         $new_array = array();
  135.        
  136.         while(count($new_array) <= $end)
  137.         {
  138.             foreach($array as $key => $value)
  139.             {
  140.                 $new_array[] = $value;
  141.             }
  142.         }
  143.        
  144.         $new_array = array_slice($new_array, 0, $end);
  145. }
  146. $duration = microtime(true)-$start;
  147. echo 'Evert: ',$duration,"\n";
  148.  
  149. $start = microtime(true);
  150. for($i = 0; $i < 100000; $i++)
  151. {
  152.     $size = 71;
  153.     $array = array('a', 'b', 'c', 'd');
  154.     $temp = call_user_func_array('array_merge', array_fill(0, ceil($size/count($array)), $array));
  155.     while(count($temp) > $size) array_pop($temp);
  156. }
  157. $duration = microtime(true)-$start;
  158. echo 'Christoph: ',$duration,"\n";
  159.  
  160. echo 'Test took ',microtime(true)-$sum,'s';