Created
December 15, 2011 22:56
-
-
Save kevinpeno/1483362 to your computer and use it in GitHub Desktop.
Test various key/value "array" type systems in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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