Skip to content

Instantly share code, notes, and snippets.

@SmellyFish
Created June 3, 2016 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmellyFish/1d84c40c1b23ef8b069234c6cb7be412 to your computer and use it in GitHub Desktop.
Save SmellyFish/1d84c40c1b23ef8b069234c6cb7be412 to your computer and use it in GitHub Desktop.
Test array vs fixed array memory usage
<?php
$limit = 100000;
$arr1 = array();
for ($i = 0; $i < $limit; $i++) {
$arr1[$i] = rand(1, $limit);
}
$startMemoryUsage = memory_get_usage();
$arr2 = array();
foreach ($arr1 as $key => $value) {
$arr2[$key] = $value;
}
$memReg = (memory_get_usage() - $startMemoryUsage);
print 'Mem usage: ' . $memReg . PHP_EOL;
unset($arr2);
$startMemoryUsage = memory_get_usage();
$arr3 = new splFixedArray($limit);
foreach ($arr1 as $key => $value) {
$arr3[$key] = $value;
}
$memImp = (memory_get_usage() - $startMemoryUsage);
print 'Mem usage: ' . $memImp . PHP_EOL;
print 'Mem gain: ' . number_format($memReg / $memImp, 2) . ' for ' . number_format($limit) . ' records';
unset($arr3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment