<?php
header('Content-type: text/plain');
$sum = microtime(true);
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
// the variables
$array = array("a", "b", "c", "d");
$desiredLength = 71;
$newArray = array();
// create a new array with AT LEAST the desired number of elements by joining the array at the end of the new array
while(count($newArray) <= $desiredLength){
$newArray = array_merge($newArray, $array);
}
// reduce the new array to the desired length (as there might be too many elements in the new array
$array = array_slice($newArray, 0, $desiredLength);
}
$duration = microtime(true)-$start;
echo 'Kau-Boy: ',$duration,"\n";
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$array = array('a', 'b', 'c', 'd');
$length = 71;
$result = array();
while(count($result) < $length)
{
$current = each($array);
if($current == false)
{
reset($array);
continue;
}
$result[] = $current[1];
}
}
$duration = microtime(true)-$start;
echo 'Frxstrem: ',$duration,"\n";
function fillArray1($length, $values) {
foreach (new InfiniteIterator(new ArrayIterator($values)) as $element) {
if (!$length--) return $result;
$result[] = $element;
}
return $result;
}
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$result = fillArray1(71, array('a', 'b', 'c', 'd'));
}
$duration = microtime(true)-$start;
echo 'NikiC: ',$duration,"\n";
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$arr = array("a", "b", "c", "d");
$len = 71;
$a = array();
$a = str_split( substr( str_repeat( join( $arr), ceil( $len / count( $arr))), 0, $len));
}
$duration = microtime(true)-$start;
echo 'user2469998: ',$duration,"\n";
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$array = array("a", "b", "c", "d");
$merge = array();
$desiredLength = 71;
while(2 * count($array) <= $desiredLength){
$array = array_merge($array, $array);
}
if($desiredLength > count($array))
$merge = array_slice($array, 0, $desiredLength - count($array));
$array = array_merge($array, $merge);
$array = array_slice($array, 0, $desiredLength);
}
$duration = microtime(true)-$start;
echo 'Alexander: ',$duration,"\n";
$start = microtime(true);
for($n = 0; $n < 100000; $n++)
{
$newarray = array();
$i = 0;
$oldarrayvalues = array("a", "b", "c", "d");
$oldarraysize = count($oldarrayvalues);
if ( $oldarraysize ) {
while ( count($newarray) < 71 ) {
$newarray[] = $oldarrayvalues[$i];
$i++;
$i %= $oldarraysize;
}
}
}
$duration = microtime(true)-$start;
echo 'Hammerite: ',$duration,"\n";
function fill(array $initalArray, $toCount) {
$initialArrayCount = count($initalArray);
$fillUp = function(array $filledUpArray, $missingCount)
use(&$fillUp, $initalArray, $initialArrayCount, $toCount)
{
if($missingCount <= 0) return array_slice($filledUpArray, 0, $toCount);
return $fillUp(array_merge($filledUpArray, $initalArray), $missingCount - $initialArrayCount);
};
return $fillUp($initalArray, $toCount - $initialArrayCount);
}
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$filledArray = fill(array("a", "b", "c", "d"), 71);
}
$duration = microtime(true)-$start;
echo 'Max: ',$duration,"\n";
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$array = array('a', 'b', 'c', 'd');
$end = 71;
$new_array = array();
while(count($new_array) <= $end)
{
foreach($array as $key => $value)
{
$new_array[] = $value;
}
}
$new_array = array_slice($new_array, 0, $end);
}
$duration = microtime(true)-$start;
echo 'Evert: ',$duration,"\n";
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
{
$size = 71;
$array = array('a', 'b', 'c', 'd');
$temp = call_user_func_array('array_merge', array_fill(0, ceil($size/count($array)), $array));
while(count($temp) > $size) array_pop($temp);
}
$duration = microtime(true)-$start;
echo 'Christoph: ',$duration,"\n";
echo 'Test took ',microtime(true)-$sum,'s';