结论
serializen的效率远高于json,特别是在反序列化方面。
实地测试
对同一个巨大数组分别进行10000次序列化和反序列化,得到如下测试结果:
测试环境
CPU: Intel i5-3317U
PHP: 7.0.12
SRV: Apache 2
json_encode 2.7458431720734 s
json_decode 7.1210720539093 s
serialize 1.899829864502 s
unserialize 0.033617973327637 s
测试代码
<?php
$de=[
"ok"=>23421344234,
"d3"=>['32af','23rdsf','fa3fawe','r23dsds'],
'asdfasfd'=>['sadfasdf','asdfdsf'=>'asdfa爱的色放撒地方发生但凡sfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfasfd1'=>['sadfa安抚仨人货币补偿现在vcxzvcxzsdf','asg发多少了空间dfdsf'=>'asdfasfdsdfsfa','asdf爱居兔肉色很尴尬人个人sadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfasfd2'=>['sadfasdf','asdfdsf'=>'asdfasfds阿斯顿发生dfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfasfd3'=>['sadf爱迪生范德萨发放第三asdf','asdfdsf'=>'asdfasfd阿萨德发的顺丰sdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfasfd4'=>['sadfasdf','asdfdsf'=>'asdfasfdsd阿什顿发发生的fsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfahgewrasfd'=>['sadfasdf','asdfdsf'=>'asdfa阿什顿发斯蒂芬sfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdffsadasfd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdaefdssfasfd'=>['sadfasdf','asdf阿斯顿发发生的dsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfa阿斯顿发发呆爽肤水对方43wefrasfd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'as阿斯蒂芬范德萨范德萨dfsdaf']],
'asdfaasdfsfd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'afasdfsdfasfd'=>['sadfasdf','asdfdsf'=>'asdfas阿斯顿发发生的分fdsdfsfa','as爱的色放范德萨dfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
'asdfas阿什顿发斯蒂芬法第三asdfsdffd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
array (
'battle_id'=> 257
,'user_id'=> 41248
,'user阿什顿发斯蒂芬多撒_id2'=> 23989
,'player'=> 41248
,'formation'=> Array
(
'41248'=> 1
,'23989'=> 2
)
,'result'=> 1
,'battle_type'=> 1
,'speed'=> Array
(
'41248'=> 0
,'23989'=> 0
)
)
];
$de=[
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
$de,
];
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
$en=json_encode($de);
}
$etime = microtime(true);
echo "json_encode :", ($etime - $stime) ,'<br/>';
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
$de=json_decode($en);
}
$etime = microtime(true);
echo "json_decode :", ($etime - $stime) ,'<br/>';
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
$en=serialize($de);
}
$etime = microtime(true);
echo "serialize :", ($etime - $stime) ,'<br/>';
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
$de=serialize($en);
}
$etime = microtime(true);
echo "unserialize :", ($etime - $stime) ,'<br/>';
转载请注明出处
《PHP中serializen()与json_encode()的性能差异》https://www.ywlib.com/archives/119.html (from 一闻自习室)
本文固定链接
https://www.ywlib.com/archives/119.html
标签
php, serializen, json
代码都写错了,serialize 写了两次
[...]https://www.ywlib.com/archives/119.html[...]