<?php
$array = array (1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456, 1, 4, 5, 6, 7, 74, 534, 653, 456, 3456, 3456, 4356, 34, 5634, 56, 4356, 4362, 462, 346, 23465, 2346, 2456);
$sum = 0;
//Count INside loop declaration
$time1 = microtime(true);
for ($i = 0; $i < 10000; $i++){
for ($j = 0; $j < count($array); $j++){
$sum += $array[$j];
}
}
$time2 = microtime(true);
echo 'Counting inside the loop = ';
$inside_time = $time2 - $time1;
echo $inside_time, " sec \n\n";
//Count OUTside loop declaration
$time1 = microtime(true);
for ($i = 0; $i < 10000; $i++){
$count = count($array);
for ($j = 0; $j < $count; $j++){
$sum += $array[$j];
}
}
$time2 = microtime(true);
echo 'Counting outside the loop = ';
$outside_time = $time2 - $time1;
echo $outside_time , " sec \n\n";
//Result
echo round($outside_time/$inside_time, 2)*100, '% performance increase';
?>