Skip to content

Instantly share code, notes, and snippets.

@kevinpeno
Created December 15, 2011 22:56
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 kevinpeno/1483362 to your computer and use it in GitHub Desktop.
Save kevinpeno/1483362 to your computer and use it in GitHub Desktop.
Test various key/value "array" type systems in PHP
<?php
# Normal array
$startMemory = memory_get_usage();
$array = range( 1, 100000 );
echo ( ( memory_get_usage() - $startMemory ) / 100000 ), " bytes per element\n";
unset( $array, $startMemory );
# stdClass as a simple map/dictionary
$startMemory = memory_get_usage();
$array = new stdClass();
for ($i = 0; $i < 100000; ++$i) {
$array->{$i} = $i;
}
echo ( ( memory_get_usage() - $startMemory ) / 100000 ), " bytes per element\n";
unset( $array, $startMemory );
#SplFixedArray
$startMemory = memory_get_usage();
$array = new SplFixedArray(100000);
for ($i = 0; $i < 100000; ++$i) {
$array[$i] = $i;
}
echo ( ( memory_get_usage() - $startMemory ) / 100000 ), " bytes per element\n";
unset( $array, $startMemory );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment