- microtime: documentation ( source)
- memory_get_usage: documentation ( source)
- printf: documentation ( source)
<?php
/**
* Object hashing tests.
*/
$sos = new SplObjectStorage();
$usage = "php %s INTEGER \t - Run the SPLObjectStorage vs. Array test with INTEGER size.\n";
$docs = array();
$iterations = 100000; //100000;
echo "Count of DOMDocuments: {$iterations}" . PHP_EOL. PHP_EOL;
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(true);
foreach ($docs as $d) {
$sos->contains($d);
}
$finis = microtime(true);
$time_to_check = $finis - $start;
$mem_spl = memory_get_usage();
$mem_used = $mem_spl - $mem_empty;
printf("SplObjectStorage:\nTime to fill: %0.12f. [s]\nTime to check: %0.12f. [s]\nMemory: %d [MB]\n\n", $time_to_fill, $time_to_check, $mem_used/1024/1024);
unset($sos);
$mem_empty = memory_get_usage();
// Test arrays:
$start = microtime(TRUE);
$arr = new SplFixedArray($iterations);
// Load the array
$_i = 0;
foreach ($docs as $d) {
$arr[$_i] = $d;
$_i++;
}
$finis = microtime(TRUE);
$time_to_fill = $finis - $start;
// Check membership on the array
$start = microtime(FALSE);
$_i = 0;
foreach ($docs as $d) {
isset($arr[$_i]);
$_i++;
}
$finis = microtime(FALSE);
$time_to_check = $finis - $start;
$mem_arr = memory_get_usage();
$mem_used = $mem_arr - $mem_empty;
printf("SplFixedArray:\nTime to fill: %0.12f. [s]\nTime to check: %0.12f. [s]\nMemory: %d [MB]\n\n", $time_to_fill, $time_to_check, $mem_used/1024/1024);
$mem_empty = memory_get_usage();
// Test arrays:
$start = microtime(TRUE);
$arr = array();
$_i =0;
// Load the array
foreach ($docs as $d) {
$arr[$_i] = $d;
$_i++;
}
$finis = microtime(TRUE);
$time_to_fill = $finis - $start;
// Check membership on the array
$start = microtime(FALSE);
$_i =0;
foreach ($docs as $d) {
isset($arr[$_i]);
$_i++;
}
$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. [s]\nTime to check: %0.12f. [s]\nMemory: %d [MB]\n\n", $time_to_fill, $time_to_check, $mem_used/1024/1024);
?>