Possible Duplicate:
What’s better at freeing memory with PHP: unset() or $var = null
This question is sorta a follow up to What's better at freeing memory with PHP: unset() or $var = null
Long story short, my own benchmarks seem to contradict the answer given in that question. My question is... why? Is the answer wrong or is there something I'm just not understanding?
<?php
$start = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
$a = 'a';
$a = NULL;
}
$elapsed = microtime(true) - $start;
echo "took $elapsed seconds\r\n";
$start = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
$a = 'a';
unset($a);
}
$elapsed = microtime(true) - $start;
echo "took $elapsed seconds\r\n";
?>
Per that it seems like "= null" is faster.
PHP 5.4 results:
- took 0.88389301300049 seconds
- took 2.1757180690765 seconds
PHP 5.3 results:
- took 1.7235369682312 seconds
- took 2.9490959644318 seconds
PHP 5.2 results:
- took 3.0069220066071 seconds
- took 4.7002630233765 seconds
PHP 5.1 results:
- took 2.6272349357605 seconds
- took 5.0403649806976 seconds
Things start to look different with PHP 5.0 and 4.4.
5.0:
- took 10.038941144943 seconds
- took 7.0874409675598 seconds
4.4:
- took 7.5352551937103 seconds
- took 6.6245851516724 seconds
Keep in mind microtime(true) doesn't work in PHP 4.4 so I had to use the microtime_float example given in php.net/microtime / Example #1.