public
Last active

Compares multidimensional, flat and string access

  • Download Gist
benchmark.php
PHP
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
<?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() {}
results
1 2 3 4 5 6 7 8
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.