I did some testing to see the speed differences between serialize and json_encode, and my results with 250 iterations are:
PHP serialized in 0.0651714730263 seconds average
JSON encoded in 0.0254955434799 seconds average
json_encode() was roughly 155.62% faster than serialize()
Test took 27.2039430141 seconds with 300 iretations.
PHP serialized in 0.0564563179016 seconds average
JSON encoded in 0.0249140485128 seconds average
json_encode() was roughly 126.60% faster than serialize()
Test took 24.4148340225 seconds with 300 iretations.
From all my tests it looks like json_encode is on average about 120% faster (sometimes it gets to about 85% and sometimes to 150%).
Here is the PHP code you can run on your server to try it out:
<?php
function fillArray($depth, $max){
static $seed;
if (is_null($seed)){
$seed = array('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10);
}
if ($depth < $max){
$node = array();
foreach ($seed as $key){
$node[$key] = fillArray( $depth + 1, $max );
}
return $node;
}
return 'empty';
}
function testSpeed($testArray, $iterations = 100){
$json_time = array();
$serialize_time = array();
$test_start = microtime(true);
for ($x = 1; $x <= $iterations; $x++){
$start = microtime(true);
json_encode($testArray);
$json_time[] = microtime(true) - $start;
$start = microtime(true);
serialize($testArray);
$serialize_time[] = microtime(true) - $start;
}
$test_lenght = microtime(true) - $test_start;
$json_average = array_sum($json_time) / count($json_time);
$serialize_average = array_sum($serialize_time) / count($serialize_time);
$result = "PHP serialized in ".$serialize_average." seconds average<br>";
$result .= "JSON encoded in ".$json_average." seconds average<br>";
if ($json_average < $serialize_average){
$result .= "json_encode() was roughly ".number_format( ($serialize_average / $json_average - 1 ) * 100, 2 )."% faster than serialize()<br>";
} else if ( $serializeTime < $jsonTime ){
$result .= "serialize() was roughly ".number_format( ($json_average / $serialize_average - 1 ) * 100, 2 )."% faster than json_encode()<br>";
} else {
$result .= "No way!<br>";
}
$result .= "Test took ".$test_lenght." seconds with ".$iterations." iterations.";
return $result;
}
echo testSpeed(fillArray(0, 5), 250);
?>