Archive - May 23, 2007

Date

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...

Honey Pot that kill bots