Skip to content

Instantly share code, notes, and snippets.

@pvankouteren
Created January 25, 2017 10:24
Show Gist options
  • Save pvankouteren/caaac936975d439aa50ee55ffe3f2d1d to your computer and use it in GitHub Desktop.
Save pvankouteren/caaac936975d439aa50ee55ffe3f2d1d to your computer and use it in GitHub Desktop.
<?php
$maxSize = (int) $_GET['size'];
$times = (int) $_GET['times'];
set_time_limit(0);
echo "<h2>Number of repeated tests: " . $times . "</h2>";
echo "<table border='1'>";
echo "<thead><tr><th>Items</th><th>Time Array</th><th>Memory Array</th><th>SplFixedArray</th><th>Memory SplFixedArray</th><th>Array/SplFixedArray ratio</th><th>Speed increase by SplFixedArray</th><th>Memory reduction by SplFixedArray</th></tr></thead>";
echo "<tbody>";
for($size = 1000; $size < $maxSize; $size *= 2) {
echo "<tr><td align='right'>" . $size . "</td>";
$arrTotal = 0;
$arrMemUsage = 0;
for($time = 0; $time < $times; $time++){
$mStart = memory_get_usage();
$container1 = array();
for($s = microtime(true), $i = 0; $i < $size; $i++) {
$container1[$i] = 1;
}
$arrMemUsage += (memory_get_usage() - $mStart);
$arrTime = (microtime(true) - $s);
$arrTotal += $arrTime;
}
$avgArrMem = ($arrMemUsage / $times);
$avgArr = ($arrTotal / $times);
echo "<td align='right'>" . $avgArr . "</td>";
echo "<td align='right'>" . $avgArrMem . "</td>";
// Cleanup to REDUCE the influence of memory blocks on the result
unset($arrTotal);
unset($arrMemUsage);
unset($container1);
$splFixedArrTotal = 0;
$splFixedArrMemUsage = 0;
for($time = 0; $time < $times; $time++){
$mStart = memory_get_usage();
$container2 = new SplFixedArray($size);
for($s = microtime(true), $i = 0; $i < $size; $i++) {
$container2[$i] = 1;
}
$splFixedArrMemUsage += (memory_get_usage() - $mStart);
$splFixedArrTime = (microtime(true) - $s);
$splFixedArrTotal += $splFixedArrTime;
}
$avgSplFixedArrMem = ($splFixedArrMemUsage / $times);
$avgSplFixedArr = ($splFixedArrTotal / $times);
echo "<td align='right'>" . $avgSplFixedArr . "</td>";
echo "<td align='right'>" . $avgSplFixedArrMem . "</td>";
// Cleanup to REDUCE the influence of memory blocks on the result
unset($splFixedArrTotal);
unset($splFixedArrMemUsage);
unset($container2);
// Calculate ratio
echo "<td align='right'>" . ($avgArr / $avgSplFixedArr) . "</td>";
// Calculate speed increase percentage
echo "<td align='right'>" . number_format(((($avgSplFixedArr - $avgArr) / $avgArr) * -100), 4) . " %</td>";
// Calculated memory reduction percentage
if ($avgArrMem == 0){
echo "<td align='right'>NaN</td></tr>";
}
else {
echo "<td align='right'>" . number_format(((($avgSplFixedArrMem - $avgArrMem) / $avgArrMem) * -100), 4) . " %</td></tr>";
}
// Cleanup to REDUCE the influence of memory blocks on the result
unset($avgArr);
unset($avgSplFixedArr);
}
echo "</tbody></table>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment