测试代码如下
<?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,
),
);
$a = '{"order_id":100004479,"order_original_id":0,"from_shop_id":0,"shop_id":200,"shipment_id":9,"order_title":"\u6d4b\u8bd5\u5973\u88c5","goods":{"495":[{"goods_id":495,"product_sn":"","product_id":0,"original_product_id":0,"quantity":1,"self_quantity":0,"price":"21.00","amount":21,"level_discount":100,"img":"2016\/03\/15\/Fn50RKk0-JZ2fJeUp3SZNL6GcPGK.jpg","title":"\u6d4b\u8bd5\u5973\u88c5","weight":"0.800","volume":"0.500","props":"","stock_type":0,"self_shipment_fee":2,"self_shipment_id":9,"self_postage":"0.00","self_shipment_info":{"start_standard":500,"start_fee":"2.00","add_standard":500,"add_fee":"1.00","valuation_type":1},"collect_fields":"","original_id":494}]},"job":[{"shipment_id":10,"order_title":"\u6d4b\u8bd5\u5973\u88c5","from_shop_id":200,"from_supplier_id":263,"shop_id":183,"supplier_id":246,"goods":{"494":[{"goods_id":494,"product_sn":"","product_id":0,"original_product_id":0,"quantity":1,"self_quantity":1,"price":"18.00","amount":18,"level_discount":"100.00","img":"2016\/03\/15\/Fn50RKk0-JZ2fJeUp3SZNL6GcPGK.jpg","title":"\u6d4b\u8bd5\u5973\u88c5","weight":"0.800","volume":"0.500","props":"","self_shipment_fee":3,"self_shipment_id":10,"self_postage":"0.01","self_shipment_info":{"start_standard":500,"start_fee":"2.00","add_standard":500,"add_fee":"1.00","valuation_type":1},"original_id":494,"supplier_id":246,"from_supplier_id":263,"collect_fields":""}]},"supplier_shipment_fee":3,"goods_amount":18,"amount":21}],"shipment_fee":5,"goods_amount":21,"amount":26,"callback_job":[{"trigger":"pay","service":"SupplierOrderReturnService","action":"postOrderPaid","callback_data":{"original_order_id":0}},{"trigger":"cancel","service":"SupplierOrderReturnService","action":"postOrderCancel","callback_data":{"original_order_id":0}}],"original_order_id":100004479}';
$target = json_decode($a, true);
$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.';
?>
由以上测试结果可知
json_encode 要比serialize所占的空间要小
但是在json_decodne的时候耗费时间要比unserialize要多
所以二者的选择就要看是是要速度还是要空间了