时间:2021-07-01 10:21:17 帮助过:32人阅读
json : 190
serialize : 257
json_encode : 0.08364200592041
json_decode : 0.18004894256592
serialize : 0.063642024993896
unserialize : 0.086990833282471
DONE.
以下是PHP 5.3.2环境其中一次测试结果:
代码如下:
json : 190
serialize : 257
json_encode : 0.062805891036987
json_decode : 0.14239192008972
serialize : 0.048481941223145
unserialize : 0.05927300453186
DONE.
这次实验得到的结论是:
json_encode和json_decode的效率并没有比serialize和unserialize的效率高,在反序列化的时候性能相差两倍左右,PHP 5.3执行效率比PHP 5.2略有提升。
以下是我用来做测试的代码:
代码如下:
<?php
$target = array (
'name' => '全能头盔',
'quality' => 'Blue',
'ti_id' => 21302,
'is_bind' => 1,
'demand_conditions' =>
array (
'HeroLevel' => 1,
),
'quality_attr_sign' =>
array (
'HeroStrength' => 8,
'HeroAgility' => 8,
'HeroIntelligence' => 8,
),
);
$json = json_encode($target);
$seri = serialize($target);
echo "json :\t\t" . strlen($json) . "\r\n";
echo "serialize :\t" . strlen($seri) . "\r\n\r\n";
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_encode($target);
}
$etime = microtime(true);
echo "json_encode :\t" . ($etime - $stime) . "\r\n";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_decode($json);
}
$etime = microtime(true);
echo "json_decode :\t" . ($etime - $stime) . "\r\n\r\n";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
serialize($target);
}
$etime = microtime(true);
echo "serialize :\t" . ($etime - $stime) . "\r\n";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
unserialize($seri);
}
$etime = microtime(true);
echo "unserialize :\t" . ($etime - $stime) . "\r\n\r\n";
echo 'DONE.';
?>