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
";
$start = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
$a = 'a';
unset($a);
}
$elapsed = microtime(true) - $start;
echo "took $elapsed seconds
";
?>
Per that it seems like "= null" is faster.
PHP 5.4 results:
PHP 5.3 results:
PHP 5.2 results:
PHP 5.1 results:
Things start to look different with PHP 5.0 and 4.4.
5.0:
4.4:
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.
Read the selected answer more carefully:
If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.
So, if all you're doing is the memory stuff, yes, that will be faster. But in a real application you may find that it slows things down more.