note 117333 added to function.serialize
| From: | MiChAeLoKGB | Date: | Sat, 23 May 2015 05:28:13 +0000 |
| Subject: | note 117333 added to function.serialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202644@lists.php.net to get a copy of this message | ||
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
// fillArray function myde by Peter Bailey
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;
}
// Change the number of iterations (250) to lower if you exceed your maximum execution time
echo testSpeed(fillArray(0, 5), 250);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 195.68.235.195
----
Manual Page -- http://php.net/manual/en/function.serialize.php
Edit -- https://master.php.net/note/edit/117333
Del: integrated -- https://master.php.net/note/delete/117333/integrated
Del: useless -- https://master.php.net/note/delete/117333/useless
Del: bad code -- https://master.php.net/note/delete/117333/bad+code
Del: spam -- https://master.php.net/note/delete/117333/spam
Del: non-english -- https://master.php.net/note/delete/117333/non-english
Del: in docs -- https://master.php.net/note/delete/117333/in+docs
Del: other reasons-- https://master.php.net/note/delete/117333
Reject -- https://master.php.net/note/reject/117333
Search -- https://master.php.net/manage/user-notes.php