Created
January 4, 2013 15:36
-
-
Save technosophos/4453518 to your computer and use it in GitHub Desktop.
SPLObjectStorage vs. PHP Arrays
This file contains hidden or 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 | |
| /** | |
| * Object hashing tests. | |
| */ | |
| $sos = new SplObjectStorage(); | |
| $docs = array(); | |
| $iterations = 100000; | |
| for ($i = 0; $i < $iterations; ++$i) { | |
| $doc = new DOMDocument(); | |
| //$doc = new stdClass(); | |
| $docs[] = $doc; | |
| } | |
| $start = $finis = 0; | |
| $mem_empty = memory_get_usage(); | |
| // Load the SplObjectStorage | |
| $start = microtime(TRUE); | |
| foreach ($docs as $d) { | |
| $sos->attach($d); | |
| } | |
| $finis = microtime(TRUE); | |
| $time_to_fill = $finis - $start; | |
| // Check membership on the object storage | |
| $start = microtime(FALSE); | |
| foreach ($docs as $d) { | |
| $sos->contains($d); | |
| } | |
| $finis = microtime(FALSE); | |
| $time_to_check = $finis - $start; | |
| $mem_spl = memory_get_usage(); | |
| $mem_used = $mem_spl - $mem_empty; | |
| printf("SplObjectStorage:\nTime to fill: %0.12f.\nTime to check: %0.12f.\nMemory: %d\n\n", $time_to_fill, $time_to_check, $mem_used); | |
| unset($sos); | |
| $mem_empty = memory_get_usage(); | |
| // Test arrays: | |
| $start = microtime(TRUE); | |
| $arr = array(); | |
| // Load the array | |
| foreach ($docs as $d) { | |
| $arr[spl_object_hash($d)] = $d; | |
| } | |
| $finis = microtime(TRUE); | |
| $time_to_fill = $finis - $start; | |
| // Check membership on the array | |
| $start = microtime(FALSE); | |
| foreach ($docs as $d) { | |
| //$arr[spl_object_hash($d)]; | |
| isset($arr[spl_object_hash($d)]); | |
| } | |
| $finis = microtime(FALSE); | |
| $time_to_check = $finis - $start; | |
| $mem_arr = memory_get_usage(); | |
| $mem_used = $mem_arr - $mem_empty; | |
| printf("Arrays:\nTime to fill: %0.12f.\nTime to check: %0.12f.\nMemory: %d\n\n", $time_to_fill, $time_to_check, $mem_used); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment