Skip to content

Instantly share code, notes, and snippets.

@davidkryzaniak
Last active October 12, 2015 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidkryzaniak/4046850 to your computer and use it in GitHub Desktop.
Save davidkryzaniak/4046850 to your computer and use it in GitHub Desktop.
PHP Explode VS Unserialize (Who's Faster?)
<?php
//This is the list of elements. There are 500 items in this array.
/* From PHP.net */
function convert($size){
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
$start1 = microtime(true);
$start1mem = memory_get_usage();
$explodeThisArray = explode($explodeThis,",");
echo "Explode took ".(microtime(true)-$start1)." Seconds<br/>";
echo "Explode used ".convert(memory_get_usage()-$start1mem)." of memory<br/><br/>";
//empty it from memory
$explodeThisArray = array();
$start = microtime(true);
$startmem = memory_get_usage();
$unserializeThis = unserialize($unserializeThis);
echo "Unserialize took ".(microtime(true)-$start)." Seconds<br/>";
echo "Unserialize used ".convert(memory_get_usage()-$startmem)." of memory<br/><br/>";
//empty it from memory
$unserializeThis = array();
$start3 = microtime(true);
$start3mem = memory_get_usage();
$jsonDecodeThis = json_decode($jsonDecodeThis);
echo "JSON Decode took ".(microtime(true)-$start3)." Seconds<br/>";
echo "JSON Decode used ".convert(memory_get_usage()-$start3mem)." of memory<br/><br/>";
//empty it from memory
$jsonDecodeThis = array();
@resetko
Copy link

resetko commented Mar 10, 2015

$explodeThisArray = explode($explodeThis,",");
array explode ( string $delimiter , string $string [, int $limit ] )

delimiter must be first parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment