7

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.

Community
  • 1
  • 1

1 Answers1

2

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.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • 7
    That honestly doesn't make any sense to me. I mean, what, does unset magically not execute any assembly instructions whereas $whatever = null; does? The answer, as given, is about as useful as saying "$whatever = null resets the buffer and the L1 cache whereas unset clears the buffer and resets the L2 cache". Techno mumbo jumbo doesn't constitute an answer. –  Dec 01 '12 at 04:18
  • 2
    @ansur I took Mark's answer to mean: If you're reusing the same pointer over and over (allocating memory to the variable) and _that's all_, then `$var = null` will be faster. I'm assuming this is because `unset()` has some implicit overhead when it hands the memory job over to the garbage collector rather than clearing it explicitly (with `$var = null`). Where `unset()` has an advantage is if you're writing "real code" that doesn't reuse the same variable over and over, and you're just trying to tell PHP "Hey, I don't need `$var` anymore. Delete it when it makes sense." – Andrew Ensley May 01 '13 at 15:42
  • @Andrew, Nope. **Both** `unset` and `$var=null` tells PHP that you don't need the value of `$var` anymore, delete it when it makes sense. None of that has any explicit nor implicit link to the GC. The only difference between this two is what would be observed, at the logical symbol table. See http://stackoverflow.com/a/13667734/632951 – Pacerier Sep 02 '15 at 06:12