详解php中implode explode serialize json msgpack性能对比

吾爱主题 阅读:153 2021-11-11 16:23:00 评论:0

测试方法

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

创建代码如下:

php" id="highlighter_932433"> ?
1 2 3 4 5 6 7 8 9 10 11 <?php $arr = array (      'content1' => '一二三四五六七八九十' ,      'content2' => '一二三四五六七八九十' ,      'content3' => '一二三四五六七八九十' ); echo file_put_contents ( 'implode.txt' , implode( ',' , $arr ), true). '<br>' ; echo file_put_contents ( 'serialize.txt' , serialize( $arr ), true). '<br>' ; echo file_put_contents ( 'json.txt' , json_encode( $arr ), true). '<br>' ; echo file_put_contents ( 'msgpack.txt' , 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小

例如:

?
1 $arr = array ( '一' , '二' , '三' , '四' , '五' , '六' , '七' , '八' , '九' , '十' );

serialize   为147字节

json_encode 为91字节

比较 implode, serialize, json_encode, msgpack_pack 性能

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?php $arr = array (      'content1' => '一二三四五六七八九十' ,      'content2' => '一二三四五六七八九十' ,      'content3' => '一二三四五六七八九十' );   $start = microtime(true); $i = 1000000; while ( $i >0){      // 分别测试运行时间及内存使用情况      $tmp = implode( ',' , $arr );      // $tmp = serialize($arr);      // $tmp = json_encode($arr);      // $tmp = msgpack_pack($arr);      $i --; } $end = microtime(true); echo 'run time:' .( $end - $start ). 's<br>' ; echo 'memory usage:' .(memory_get_usage()/1024). 'KB' ; ?>

implode       1.3225722312927s    628.50KB

serialize     2.0553789138794s    628.32KB

json_encode   2.5058920383453s    628.34KB

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

比较 explode, unserialize, json_decode, msgpack_unpack 性能

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php $data = file_get_contents ( 'implode.txt' ); //$data = file_get_contents('serialize.txt'); //$data = file_get_contents('json.txt'); //$data = file_get_contents('msgpack.txt');   $start = microtime(true); $i = 1000000; while ( $i >0){      $tmp = explode ( ',' , $data );      //$tmp = unserialize($data);      //$tmp = json_decode($data, true);      //$tmp = msgpack_unpack($data);      $i --; } $end = microtime(true); echo 'run time:' .( $end - $start ). 's<br>' ; echo 'memory usage:' .(memory_get_usage()/1024). 'KB' ; ?>

explode         1.7446749210358s    628.74KB

unserialize     2.1386790275574s    628.67KB

json_decode     5.2423169612885s    628.84KB

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

总结

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

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

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

以上就是详解php中implode/explode、serialize、json、msgpack性能对比的详细内容,更多关于php性能对比的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/fdipzone/article/details/43679337

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容