<?php
// needed to cope with a million entry array
//ini_set('memory_limit', '512M');
$limit = 500000;
$text[] = "Now is the time";
$text[] = "for all good men";
$text[] = "to come to the aid";
$text[] = "of their country.";
$text2['part1'] = "Now is the time";
$text2['part2'] = "for all good men";
$text2['part3'] = "to come to the aid";
$text2['part4'] = "of their country.";
$bigArray = fillArray($limit);
$max=count($bigArray);
#######################################################
$start = microtime(TRUE);
for($i=0; $i<$max; $i++) {
$bigArray[$i] = $text[0];
$bigArray[$i] .= ' ';
$bigArray[$i] .= $text[1];
$bigArray[$i] .= ' ';
$bigArray[$i] .= $text[2];
$bigArray[$i] .= ' ';
$bigArray[$i] .= $text[3];
}
$results['dotequal']['time'] = microtime(TRUE) - $start;
$results['dotequal']['name'] = 'Dot Equals';
#######################################################
$start = microtime(TRUE);
for($i=0; $i<$max; $i++) {
$bigArray[$i] = $text[0].' '.$text[1].' '.$text[2].' '.$text[3];
}
$results['concat']['time'] = microtime(TRUE) - $start;
$results['concat']['name'] = 'Dot Concatenation';
#######################################################
$start = microtime(TRUE);
for($i=0; $i<$max; $i++) {
$bigArray[$i] = "$text[0] $text[1] $text[2] $text[3]";
}
$results['replace']['time'] = microtime(TRUE) - $start;
$results['replace']['name'] = 'In-string Replacement';
#######################################################
$start = microtime(TRUE);
for($i=0; $i<$max; $i++) {
$bigArray[$i] = "{$text2['part1']} {$text2['part2']} {$text2['part3']} {$text2['part4']}";
}
$results['replace2']['time'] = microtime(TRUE) - $start;
$results['replace2']['name'] = 'Complex In-string Replacement';
#######################################################
$start = microtime(TRUE);
for($i=0; $i<$max; $i++) {
$bigArray[$i] = sprintf('%s %s %s %s', $text[0], $text[1], $text[2], $text[3]);
}
$results['sprintf']['time'] = microtime(TRUE) - $start;
$results['sprintf']['name'] = 'Function: sprintf()';
#######################################################
$start = microtime(TRUE);
for($i=0; $i<$max; $i++) {
$bigArray[$i] = implode(' ', $text);
}
$results['implode']['time'] = microtime(TRUE) - $start;
$results['implode']['name'] = 'Function: implode()';
#######################################################
foreach($results AS $test) {
echo $test['name']." -- ".sprintf("%01.6f", $test['time'])."\n";
}
#######################################################
function fillArray($limit) {
// build huge array
for($i=0; $i<$limit; $i++) {
$array[] = $i;
}
return $array;
}
?>
- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.7, 7.2.0
- Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 4096 bytes) in /in/BmeUo on line 33
Process exited with code 255. - Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
- Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes) in /in/BmeUo on line 104
Process exited with code 255. - Output for 5.2.3 - 5.2.17
- Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /in/BmeUo on line 105
Process exited with code 255. - Output for 5.2.1 - 5.2.2
- Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 24 bytes) in /in/BmeUo on line 104
Process exited with code 255. - Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0
Process exited with code 137.
preferences:
127.43 ms | 402 KiB | 175 Q