正儿八经的问题,为什么网上的人说 json_encode 的效率比 serialize 高,而我自己测试则相反? PHP 数组保存在文件应该选择哪种方式比较好?
我把一个 php 数组分别保存为 serialize 和 json,然后
$start = microtime(true); for($i = 0; $i < 1000; $i++){ $ser = file_get_contents('gdip.ser'); $arr = unserialize($ser); } $l = microtime(true) - $start; echo $l."n"; // $start = microtime(true); for($i = 0; $i < 1000; $i++){ $json = file_get_contents('gdip.json'); $arr = json_decode($json, true); } $l = microtime(true) - $start; echo $l."n";
然后运行得到的结果
0.939453125 2.0195319652557
这能不能说明保存成 serialize 格式读取效率更快?