Trabla: php: SUPER FAST loop through array
Solving:
Winner:
while ( list($key, $value) = each($array) )
1. php.net - http://php.net/manual/ru/function.array-walk.php
find code of "zlobnygrif at gmail dot com"
2. My test:
<?php
$MAX = 1000000;
echo '<br><b>Array with int indexes</b><br>';
$big_array = array();
echo 'Fill array<br>';
$timer = microtime(true);
for($i = 0; $i < $MAX; $i++){
$big_array[$i] = "Value of Object $i";
}
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / Fill array<br>';
echo '<br>============================<br>';
echo 'foreach ($variable as $key => $value)<br>';
$timer = microtime(true);
foreach ($big_array as $key => $value) {
$value = $value . $key;
}
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / foreach ($variable as $key => $value)<br>';
echo '<br>============================<br>';
echo 'array_walk(array, funcname)<br>';
$timer = microtime(true);
array_walk( $big_array, function($key,&$value){ $value = $value . $key; });
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / array_walk(array, funcname)<br>';
echo '<br>============================<br>';
echo 'while ( list($key, $value) = each($array) )<br>';
$timer = microtime(true);
while ( list($key, $value) = each($big_array) ){
$big_array[$key] = $value . $key;
}
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / while ( list($key, $value) = each($array) )<br>';
echo '<br>============================<br>';
echo '<br><b>Array with string indexes</b><br>';
$big_array = array();
echo 'Fill array<br>';
$timer = microtime(true);
for($i = 0; $i < $MAX; $i++){
$big_array["obj$i"] = "Value of Object $i";
}
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / Fill array<br>';
echo '<br>============================<br>';
echo 'foreach ($variable as $key => $value)<br>';
$timer = microtime(true);
foreach ($big_array as $key => $value) {
$value = $value . $key;
}
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / foreach ($variable as $key => $value)<br>';
echo '<br>============================<br>';
echo 'array_walk(array, funcname)<br>';
$timer = microtime(true);
array_walk( $big_array, function($key,&$value){ $value = $value . $key; });
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / array_walk(array, funcname)<br>';
echo '<br>============================<br>';
echo 'while ( list($key, $value) = each($array) )<br>';
$timer = microtime(true);
while ( list($key, $value) = each($big_array) ){
$big_array[$key] = $value . $key;
}
printf("%.3f sec\n", microtime(true) - $timer);
echo ' / while ( list($key, $value) = each($array) )<br>';
echo '<br>============================<br>';
echo '-----------------------------------------------------------';
?>
No comments:
Post a Comment