Skip to content

Instantly share code, notes, and snippets.

@nikic
Created February 6, 2011 10:57
Show Gist options
  • Save nikic/813289 to your computer and use it in GitHub Desktop.
Save nikic/813289 to your computer and use it in GitHub Desktop.
Compares multidimensional, flat and string access
<?php
echo '<pre>';
$w = 1000;
$h = 1000;
// generate multidimensional array
$multi = array();
for ($i = 0; $i < $w; ++$i) {
$multi[$i] = array();
for ($j = 0; $j < $h; ++$j) {
$multi[$i][$j] = true;
}
}
// measure time accessing all elements in multidimensional array
$multiMem = memory_get_usage();
$multiStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
noop($multi[$x][$y]);
}
}
$multiEnd = microtime(true);
// measure time accessing all elements in multidimensional array caching the outer loop
$cMultiMem = '--------';
$cMultiStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
$col = $multi[$x];
for ($y = 0; $y < $h; ++$y) {
noop($col[$y]);
}
}
$cMultiEnd = microtime(true);
unset($multi);
// generate fixed size multidimensional array
$fMulti = new SplFixedArray($w);
for ($i = 0; $i < $w; ++$i) {
$fMulti[$i] = new SplFixedArray($h);
for ($j = 0; $j < $h; ++$j) {
$fMulti[$i][$j] = true;
}
}
// measure time accessing all elements in fixed size multidimensional array
$fMultiMem = memory_get_usage();
$fMultiStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
noop($fMulti[$x][$y]);
}
}
$fMultiEnd = microtime(true);
// measure time accessing all elements in fixed size multidimensional array caching the outer loop
$cfMultiMem = '--------';
$cfMultiStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
$col = $fMulti[$x];
for ($y = 0; $y < $h; ++$y) {
noop($col[$y]);
}
}
$cfMultiEnd = microtime(true);
unset($fMulti);
// generate flat array
$flat = array();
for ($i = 0; $i < $w * $h; ++$i) {
$flat[$i] = true;
}
// measure time accessing all elements in flat array
$flatMem = memory_get_usage();
$flatStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
noop($flat[$x * $w + $y]);
}
}
$flatEnd = microtime(true);
unset($flat);
// generate fixed size flat array
$fFlat = new SplFixedArray($w * $h);
for ($i = 0; $i < $w * $h; ++$i) {
$fFlat[$i] = true;
}
// measure time accessing all elements in fixed size flat array
$fFlatMem = memory_get_usage();
$fFlatStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
noop($fFlat[$x * $w + $y]);
}
}
$fFlatEnd = microtime(true);
unset($fFlat);
// generate string
$str = str_repeat('1', $w * $h);
// measure time accessing all elements in string
$strMem = memory_get_usage();
$strStart = microtime(true);
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
noop($str[$x * $w + $y]);
}
}
$strEnd = microtime(true);
unset($str);
// generate integer array
$intSize = PHP_INT_SIZE * 8;
$ints = array();
for ($i = 0, $max = round($w * $h / $intSize) + 1; $i < $max; ++$i) {
$ints[$i] = 0xffffffff;
}
// measure time accessing all elements in integer array
$intsMem = memory_get_usage();
$intsStart = microtime(true);
$k = 0;
$l = 0;
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
if (++$k == $intSize) {
$k = 0;
++$l;
}
noop(($ints[$l] >> $k) & 1);
}
}
$intsEnd = microtime(true);
unset($ints);
echo 'Multi took: ', $multiEnd - $multiStart, 's, Memory usage: ', $multiMem, "\n";
echo 'cMulti took: ', $cMultiEnd - $cMultiStart, 's, Memory usage: ', $cMultiMem, "\n";
echo 'fMulti took: ', $fMultiEnd - $fMultiStart, 's, Memory usage: ', $fMultiMem, "\n";
echo 'cfMulti took: ', $cfMultiEnd - $cfMultiStart, 's, Memory usage: ', $cfMultiMem, "\n";
echo 'Flat took: ', $flatEnd - $flatStart, 's, Memory usage: ', $flatMem, "\n";
echo 'fFlat took: ', $fFlatEnd - $fFlatStart, 's, Memory usage: ', $fFlatMem, "\n";
echo 'String took: ', $strEnd - $strStart, 's, Memory usage: ',$strMem, "\n";
echo 'Ints took; ', $intsEnd - $intsStart, 's, Memory usage: ',$intsMem, "\n";
function noop() {}
Multi took: 0.28364181518555s, Memory usage: 84604840
cMulti took: 0.25144505500793s, Memory usage: --------
fMulti took: 0.28656005859375s, Memory usage: 36681568
cfMulti took: 0.24749112129211s, Memory usage: --------
Flat took: 0.28597784042358s, Memory usage: 84584128
fFlat took: 0.31990194320679s, Memory usage: 36390232
String took: 0.43286204338074s, Memory usage: 1390184
Ints took; 0.35269808769226s, Memory usage: 3021920
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment