Rumah  >  Artikel  >  pembangunan bahagian belakang  >  对php implode/explode, serialize, json, msgpack 之间性能的讲解

对php implode/explode, serialize, json, msgpack 之间性能的讲解

jacklove
jackloveasal
2018-06-09 11:07:461966semak imbas

php implode/explode, serialize, json, msgpack 性能对比

首先使用implode, serialize, json_encode, msgpack_pack创建四个文本文件,用于测试。

创建代码如下:

<?php
$arr = array(
    &#39;content1&#39; => &#39;一二三四五六七八九十&#39;,
    &#39;content2&#39; => &#39;一二三四五六七八九十&#39;,
    &#39;content3&#39; => &#39;一二三四五六七八九十&#39;
);
echo file_put_contents(&#39;implode.txt&#39;, implode(&#39;,&#39;,$arr), true).&#39;<br>&#39;;
echo file_put_contents(&#39;serialize.txt&#39;, serialize($arr), true).&#39;<br>&#39;;
echo file_put_contents(&#39;json.txt&#39;, json_encode($arr), true).&#39;<br>&#39;;
echo file_put_contents(&#39;msgpack.txt&#39;, msgpack_pack($arr), true);
?>


创建后生成

implode.txt    92字节
serialize.txt   165字节
json.txt          223字节
msgpack.txt  121字节

生成的字符串大小排序如下 implode < msgpack_pack < serialize < json_encode

如果数组简单,则json_encode有可能比serialize小

例如:

$arr = array(&#39;一&#39;,&#39;二&#39;,&#39;三&#39;,&#39;四&#39;,&#39;五&#39;,&#39;六&#39;,&#39;七&#39;,&#39;八&#39;,&#39;九&#39;,&#39;十&#39;);

serialize   为147字节

json_encode 为91字节

比较 implode, serialize, json_encode, msgpack_pack 性能

<?php
$arr = array(
    &#39;content1&#39; => &#39;一二三四五六七八九十&#39;,
    &#39;content2&#39; => &#39;一二三四五六七八九十&#39;,
    &#39;content3&#39; => &#39;一二三四五六七八九十&#39;
);
$start = microtime(true);
$i = 1000000;
while($i>0){
    // 分别测试运行时间及内存使用情况
    $tmp = implode(&#39;,&#39;,$arr);
    // $tmp = serialize($arr);
    // $tmp = json_encode($arr);
    // $tmp = msgpack_pack($arr);
    $i--;
}
$end = microtime(true);
echo &#39;run time:&#39;.($end-$start).&#39;s<br>&#39;;
echo &#39;memory usage:&#39;.(memory_get_usage()/1024).&#39;KB&#39;;
?>
implode       1.3225722312927s    628.50KB
serialize     2.0553789138794s    628.32KB
json_encode   2.5058920383453s    628.34KB
msgpack_pack  1.6431028842926s    628.24KB

结果:内存使用情况差不多,运行时间 implode < msgpack_pack < serialize < json_encode

比较 explode, unserialize, json_decode, msgpack_unpack 性能

<?php
$data = file_get_contents(&#39;implode.txt&#39;);
//$data = file_get_contents(&#39;serialize.txt&#39;);
//$data = file_get_contents(&#39;json.txt&#39;);
//$data = file_get_contents(&#39;msgpack.txt&#39;);
$start = microtime(true);
$i = 1000000;
while($i>0){
    $tmp = explode(&#39;,&#39;,$data);
    //$tmp = unserialize($data);
    //$tmp = json_decode($data, true);
    //$tmp = msgpack_unpack($data);
    $i--;
}
$end = microtime(true);
echo &#39;run time:&#39;.($end-$start).&#39;s<br>&#39;;
echo &#39;memory usage:&#39;.(memory_get_usage()/1024).&#39;KB&#39;;
?>
explode         1.7446749210358s    628.74KB
unserialize     2.1386790275574s    628.67KB
json_decode     5.2423169612885s    628.84KB
msgpack_unpack  2.2290098667145s    628.63KB

结果:内存使用情况差不多,运行时间 explode < serialize < msgpack_unpack < json_decode

总结,由于implode/explode不适合使用复杂的结构,因此常用的为serialize,json,msgpack三种。

而三种比较,运行速度,内存占用,空间占用最优为msgpack, 其次是serialize,最后是json。

如有条件,建议使用msgpack序列化处理数据

关于msgpack 可以查看我之前写的文章:《MessagePack 序列化格式》

本文对php implode/explode, serialize, json, msgpack 之间性能进行比较,更多相关内容请关注php中文网。

相关推荐:

php str_replace 替换指定次数方法 的讲解

关于header,headers_sent,headers_list,header_remove 使用说明

通过PDO 查询mysql返回字段整型变为String型的解决方法

Atas ialah kandungan terperinci 对php implode/explode, serialize, json, msgpack 之间性能的讲解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn