加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php中serialize序列化与json性能测试的示例分析

发布时间:2020-12-13 06:16:20 所属栏目:PHP教程 来源:网络整理
导读:。 代码如下: $target = 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 ) ); $

代码如下:
$target = 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
)
); $json = json_encode($target);
$seri = serialize($target); echo "json :",strlen($json),'
';
echo "serialize :",strlen($seri),'
'; $stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
json_encode($target);
}
$etime = microtime(true); echo "json_encode :",($etime - $stime),'
'; //---------------------------------- $stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
json_decode($json,true);
}
$etime = microtime(true); echo "json_decode :",'
'; //----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
serialize($target);
}
$etime = microtime(true); echo "serialize :",'
'; //----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
unserialize($seri);
}
$etime = microtime(true); echo "unserialize :",'
'; ?>

测试结果:






serialize :8590
json_encode :0.90479207038879
json_decode :1.753741979599
serialize :1.3566699028015
unserialize :1.3003630638123

原因:

  • serialize稍微差一点

    json无法处理对象方法等数据。

    • 在前后端交互一般都使用JSON,另外,

代码如下:

'battle_id'=> 257
,'formation'=> Array ('41248'=> 1,'23989'=> 2)
,'speed'=> Array( '41248'=> 0,'23989'=> 0 )
,'attacker'=> Array(
'1'=> Array (
'user_id'=> 41248
,'soldier_id'=> 28
,'prototype_id'=> 4
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3997
,'hp'=> 3997
,'attack_general'=> 346
,'attack_skill'=> 596
,'attack_explode'=> 458
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 2
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'4'=> Array (
'user_id'=> 41248
,'soldier_id'=> 29
,'prototype_id'=> 2
,'totalhp'=> 3555
,'hp'=> 3555
,'attack_general'=> 396
,'attack_skill'=> 581
,'attack_explode'=> 418
,'block'=> 0
,'foramtion_effect'=> 0
)
,'5'=> Array (
'user_id'=> 41248
,'soldier_id'=> 30
,'prototype_id'=> 6
,'totalhp'=> 3043
,'hp'=> 3043
,'attack_general'=> 351
,'attack_skill'=> 540
,'attack_explode'=> 474
,'7'=> Array (
'user_id'=> 41248
,'soldier_id'=> 37
,'totalhp'=> 3491
,'hp'=> 3491
,'attack_general'=> 393
,'attack_skill'=> 532
,'attack_explode'=> 456
,'foramtion_effect'=> 0 ))
,'defender'=> Array(
'2'=> Array(
'user_id'=> 23989
,'soldier_id'=> 24
,'prototype_id'=> 1
,'totalhp'=> 3230
,'hp'=> 3230
,'attack_general'=> 390
,'attack_skill'=> 567
,'attack_explode'=> 442
,'5'=> Array(
'user_id'=> 23989
,'soldier_id'=> 25
,'totalhp'=> 3400
,'hp'=> 3400
,'attack_general'=> 379
,'attack_skill'=> 536
,'attack_explode'=> 405
,'foramtion_effect'=> 0 )
,'7'=> Array(
'user_id'=> 23989
,'soldier_id'=> 26
,'totalhp'=> 3669
,'hp'=> 3669
,'attack_general'=> 362
,'attack_skill'=> 549
,'attack_explode'=> 426
,'9'=> Array(
'user_id'=> 23989
,'soldier_id'=> 27
,'totalhp'=> 3618
,'hp'=> 3618
,'attack_general'=> 326
,'attack_skill'=> 510
,'attack_explode'=> 419
,'foramtion_effect'=> 0) )
,'battle_process'=> Array(
'0'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
) ,'1'=> Array(
'user_id'=> 41248
,'2'=> Array(
'user_id'=> 41248
,'state'=> 0
)
,'3'=> Array(
'user_id'=> 41248
,'4'=> Array(
'user_id'=> 41248
,'5'=> Array(
'user_id'=> 41248
,'6'=> Array(
'user_id'=> 41248
,'7'=> Array(
'user_id'=> 41248
,'8'=> Array(
'user_id'=> 41248
,'9'=> Array(
'user_id'=> 41248
,'10'=> Array(
'user_id'=> 41248
,'11'=> Array(
'user_id'=> 41248
,'12'=> Array(
'user_id'=> 41248
,'13'=> Array(
'user_id'=> 41248
,'14'=> Array(
'user_id'=> 41248
,'15'=> Array(
'user_id'=> 41248
,'16'=> Array(
'user_id'=> 41248
,'17'=> Array(
'user_id'=> 41248
,'18'=> Array(
'user_id'=> 41248
,'19'=> Array(
'user_id'=> 41248
,'20'=>Array(
'user_id'=> 41248
,'state'=> 0
)
) );
$json = json_encode($target);
$seri = serialize($target); echo "json :",'
';
echo "serialize :",'
'; $stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
json_encode($target);
}
$etime = microtime(true); echo "json_encode :",'
'; //---------------------------------- $stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
json_decode($json,true);
}
$etime = microtime(true); echo "json_decode :",'
'; //----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
serialize($target);
}
$etime = microtime(true); echo "serialize :",'
'; //----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++) {
unserialize($seri);
}
$etime = microtime(true); echo "unserialize :",'
'; ?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读