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

json :156
serialize :222
json_encode :0.1087498664856
json_decode :0.12652111053467
serialize :0.041656017303467
unserialize :0.040987968444824

测试结果看出json效率稍微比serialize差点,在php5.2可能会更差。应该是在php5.3之后,json扩展做了优化。

然后使用大数组做测试(代码放到最后,因为代码的数组比较长):

测试结果:

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

我们可以看出,serialize比json差了快一个数量级。

总结:

1) 空间的比较

serialize在编码后大概是json的1.5倍

原因:

serialize后字符串包含了子串的长度,这可能是速度方面的优化,但是测试结果不尽人意 serialize有更加详细的类型区分,而json只有四种类型,并且是以简单的符号表示。 2)速度的比较 在较小数据的情况下, serialize比json快数量级。

在大数据量的情况下,json比serialize稍微差一点

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

4)使用范围

序列化使用serialize,特别是对象的存储。这是其存在的意义。 与对象无关的数据存储可以使用json,如包含大量数字的数组等。 在前后端交互一般都使用JSON,另外,目前JSON只支持UTF-8编码的数据。

复制代码 代码如下:


 <?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  ) 
   ,'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 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3555 
                   ,'hp'=> 3555 
                   ,'attack_general'=> 396 
                   ,'attack_skill'=> 581 
                   ,'attack_explode'=> 418 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0 
                ) 
           ,'5'=> Array                ( 
                   'user_id'=> 41248 
                   ,'soldier_id'=> 30 
                   ,'prototype_id'=> 6 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3043 
                   ,'hp'=> 3043 
                   ,'attack_general'=> 351 
                   ,'attack_skill'=> 540 
                   ,'attack_explode'=> 474 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0) 
           ,'7'=> Array ( 
                   'user_id'=> 41248 
                   ,'soldier_id'=> 37 
                   ,'prototype_id'=> 2 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3491 
                   ,'hp'=> 3491 
                   ,'attack_general'=> 393 
                   ,'attack_skill'=> 532 
                   ,'attack_explode'=> 456 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0   )) 
   ,'defender'=> Array( 
           '2'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 24 
                   ,'prototype_id'=> 1 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3230 
                   ,'hp'=> 3230 
                   ,'attack_general'=> 390 
                   ,'attack_skill'=> 567 
                   ,'attack_explode'=> 442 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0) 
           ,'5'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 25 
                   ,'prototype_id'=> 2 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3400 
                   ,'hp'=> 3400 
                   ,'attack_general'=> 379 
                   ,'attack_skill'=> 536 
                   ,'attack_explode'=> 405 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0 ) 
           ,'7'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 26 
                   ,'prototype_id'=> 6 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3669 
                   ,'hp'=> 3669 
                   ,'attack_general'=> 362 
                   ,'attack_skill'=> 549 
                   ,'attack_explode'=> 426 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0 ) 
           ,'9'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 27 
                   ,'prototype_id'=> 1 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3618 
                   ,'hp'=> 3618 
                   ,'attack_general'=> 326 
                   ,'attack_skill'=> 510 
                   ,'attack_explode'=> 419 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'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 
                   ,'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 
                ) 

           ,'2'=> 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 
                ) 
           ,'3'=> 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 
                ) 

           ,'4'=> 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 
                ) 
           ,'5'=> 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 
                ) 

           ,'6'=> 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 
                ) 

           ,'7'=> 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 
                ) 
           ,'8'=> 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 
                ) 

           ,'9'=> 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 
                ) 
           ,'10'=> 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 
                ) 
           ,'11'=> 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 
                ) 

           ,'12'=> 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 
                ) 
           ,'13'=> 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 
                ) 
           ,'14'=> 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 
                ) 

           ,'15'=> 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 
                ) 
           ,'16'=> 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 
                ) 
           ,'17'=> 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 
                ) 

           ,'18'=> 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 
                ) 
           ,'19'=> 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 
                ) 

           ,'20'=>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 
                ) 
        ) 

); 

  
$json = json_encode($target); 
$seri = serialize($target); 

echo "json :" , strlen($json) ,'<br/>'; 
echo "serialize :", strlen($seri) ,'<br/>'; 

$stime = microtime(true); 
for ($i = 0; $i < 10000; $i ++) { 
    json_encode($target); 

$etime = microtime(true); 

echo "json_encode :", ($etime - $stime) ,'<br/>'; 

//---------------------------------- 

$stime = microtime(true); 
for ($i = 0; $i < 10000; $i ++) { 
   json_decode($json,true); 

$etime = microtime(true); 

echo "json_decode :", ($etime - $stime),'<br/>'; 

//---------------------------------- 
$stime = microtime(true); 
for ($i = 0; $i < 10000; $i ++) { 
    serialize($target); 

$etime = microtime(true); 

echo "serialize :", ($etime - $stime) ,'<br/>'; 

//---------------------------------- 
$stime = microtime(true); 
for ($i = 0; $i < 10000; $i ++) { 
    unserialize($seri); 

$etime = microtime(true); 

echo "unserialize :", ($etime - $stime),'<br/>'; 

?>   

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6418aeeecfcb1bc2981be837e590777c.html