[ create a new paste ] login | about

Link: http://codepad.org/oVFpsRdI    [ raw code | output | fork ]

PHP, pasted on Mar 26:
<?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';
?>


Output:
1
2
3
4
5
Counting inside the loop = 6.6968560218811 sec 

Counting outside the loop = 2.6391630172729 sec 

39% performance increase


Create a new paste based on this one


Comments: