1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
<?php // -*- C++ -*-
set_time_limit(0);
$iterations=10000;
function utime()
{
$time = explode(" ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
function print_progress ($i)
{
if ($i%400==0) {
echo "\r$i |";
flush();
} elseif ($i%400==100) {
echo "\r$i \\";
flush();
} elseif ($i%400==200) {
echo "\r$i -";
flush();
} elseif ($i%400==300) {
echo "\r$i /";
flush();
}
}
function mul($i,$j)
{
return $i*$j;
}
echo "PHP3 benchmark - $iterations iterations\n\n";
echo "Benchmarking idle while loop:\n";
$starttime=utime();
$i=1;
while ($i<=$iterations) {
print_progress($i);
$i++;
}
printf("\rDone, took %.2f seconds.\n", ($idle_while=utime()-$starttime));
echo "Benchmarking while loop with internal mul() function calls:\n";
$starttime=utime();
$i=1;
$sum=0;
while ($i<=$iterations) {
$sum += mul($i,$i);
print_progress($i);
$i++;
}
printf("\rDone, took %.2f seconds.\n", ($full_while=utime()-$starttime));
printf("That means the extra function was adding %.5f secs per iteration.\n",
(($full_while-$idle_while)/doubleval($iterations)));
//echo "\rDone, took ".($full_while=time()-$starttime)." seconds.\n";
//echo "That means the extra function was adding ".(($full_while-$idle_while)/doubleval($iterations))." seconds per iteration.\n";
echo "Benchmarking idle for loop, $iterations iterations:\n";
$starttime=utime();
for ($i=1; $i<=$iterations; $i++) {
print_progress($i);
}
printf("\rDone, took %.2f seconds.\n", ($idle_for=utime()-$starttime));
printf("That means for loops are %.2f%% slower than while loops.\n",
((doubleval($idle_for-$idle_while)*100)/$idle_while));
echo "Benchmarking for loop with internal mul() function calls:\n";
$sum=0;
$starttime=utime();
for ($i=1; $i<=$iterations; $i++) {
$sum += mul($i,$i);
print_progress($i);
}
echo "\rDone, took ".($full_for=utime()-$starttime)." seconds.\n";
printf("That means for loops are %.2f%% slower than while loops.\n",
((doubleval($full_for-$full_while)*100)/$full_while));
|