imagedestroy VS unset

Boring days drives the sanest people do craziest jobs.
Glad I'm not one of them. Still, I benchmarked imagedestroy() and unset() function with a little script:

<?php
    $cool = imagecreatefrompng('imagefile.png');
    echo memory_get_usage(),'<br />';
 
      $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
	  $cool = imagecreatefrompng('primespiral2000.png');
    //imagedestroy($cool);
    unset($cool);
      $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo bcsub($endtime,$starttime,6),'<br />';
    echo memory_get_usage(),'<br />';
    ?>

This script proves unset() uses less memory and it's the better choice. unset() result less memory is most likely because unset() actually delete the variable, while imagedestroy() only clean up the image structure inside GD.

    $cool = imagecreatefrompng('imagefile.png');
    imagedestroy($cool);
echo isset($cool); //1
 $cool = imagecreatefrompng('imagefile.png');
    unset($cool);
echo isset($cool); //echos nothing...


Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use <fn>...</fn> to insert automatically numbered footnotes.

More information about formatting options

What is 32 + 6?
To combat spam, please solve the math question above.
Honey Pot that kill bots