Advertisement:

Author Topic: [Tips & Tricks] -> BENCHMARK  (Read 2764 times)

lucky_strike

  • Guest
[Tips & Tricks] -> BENCHMARK
« on: May 24, 2013, 11:44:27 pm »
Y  invite ALL  to put some tests and results :) thanks !
Code: [Select]
require_once 'Benchmark/Iterate.php';
define(MAX_RUN, 500);
$data = array(1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
doBenchmark('v1', $data);
doBenchmark('v2', $data);
doBenchmark('v3', $data);
doBenchmark('v4', $data);
doBenchmark('v5', $data);
doBenchmark('v6', $data);
doBenchmark('v7', $data);
doBenchmark('v8', $data);
function doBenchmark($functionName = null, $arr = null)
{
reset($arr);
$benchmark = new Benchmark_Iterate;
$benchmark->run(MAX_RUN, $functionName, $arr);
$result = $benchmark->get();
echo '<br>';
printf("%s ran %d times where average exec time %.5f ms",
$functionName,
$result['iterations'],
$result['mean'] * 1000);
}
function v1($myArray = null) {
// Do bad loop
for ($i =0; $i < sizeof($myArray); $i++)
{
echo '<!--' . $myArray[$i] . ' --> ';
}
}
function v2($myArray = null) {
// Do better loop
// Get the size of array
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i++)
{
echo '<!--' . $myArray[$i] . ' --> ';
}
}
function v3($myArray = null) {
// Do much better loop
// Get the size of array
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i++)
{
// Store the output in a string
$output .= '<!--' . $myArray[$i] . ' --> ';
}
// Echo the output string
echo $output;
}
function v4($myArray = null) {
// Do much better loop
// Get the size of array
$max = sizeof($myArray);
$output = array();
for ($i =0; $i < $max ; $i++)
{
// Store the output in a string
array_push($output, '<!--' .
$myArray[$i] .
' --> '
);
}
// Echo the output string
echo implode('', $output);
}
function v5($myArray = null) {
// Do much better loop
// Get the size of array
$max = count($myArray);

for ($i =0; $i < $max ; $i++)
{
// Store the output in a string
echo '<!--' . $myArray[$i] . ' --> ';
}
// Echo the output string

}
function v6($myArray = null) {
// Do much better loop
// Get the size of array


for ($i =0; $i < count($myArray) ; $i++)
{
// Store the output in a string
echo '<!--' . $myArray[$i] . ' --> ';
}
// Echo the output string

}

function v7($myArray = null) {
// Do much better loop
// Get the size of array
foreach($myArray as $number)
{
// Store the output in a string
echo '<!--' . $number . ' --> ';
}

// Echo the output string

}

function v8($myArray = null) {
// Do much better loop
// Get the size of array
 $key = array_keys($myArray);
    $size = sizeOf($key);
for ($i=0; $i<$size; $i++)
{
// Store the output in a string
echo '<!--' . $myArray[$i]  . ' --> ';
}

// Echo the output string

}

RESULT :
Code: [Select]
v1 ran 500 times where average exec time 0.02600 ms
v2 ran 500 times where average exec time 0.01500 ms
v3 ran 500 times where average exec time 0.02300 ms
v4 ran 500 times where average exec time 0.02800 ms
v5 ran 500 times where average exec time 0.01700 ms
v6 ran 500 times where average exec time 0.02100 ms
v7 ran 500 times where average exec time 0.02200 ms
v8 ran 500 times where average exec time 0.01900 ms

lucky_strike

  • Guest
Re: [Tips & Tricks] -> BENCHMARK
« Reply #1 on: May 24, 2013, 11:45:45 pm »