热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP中的序列化方法性能对比

对比以下四个方法1、json_encode序列化后可阅读,多语言通用协议。2、serialize序列化后可阅读,非通用协议,只能用在

对比以下四个方法

1、json_encode
序列化后可阅读,多语言通用协议。2、serialize
序列化后可阅读,非通用协议,只能用在PHP3、swoole_serialize
序列化后不可阅读,非通用协议,只能用在PHP4、msgpack
序列化后不可阅读,多语言通用协议。

PHP版本 7.2.27

安装扩展

pecl install swoole_serialize-0.1.1
pecl install msgpackextension=msgpack
extension=swoole_serializephp --ri msgpack
php --ri swoole_serialize

使用 var_export($list, true) 原样导出数组

$arr = array(0 =>array('id' => 673,'userid' => 209778,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 5,'createtime' => '2020-02-17 15:26:40',),1 =>array('id' => 675,'userid' => 4887823,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 5,'createtime' => '2020-02-17 20:32:17',),2 =>array('id' => 680,'userid' => 4987463,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 5,'createtime' => '2020-02-18 17:45:58',),3 =>array('id' => 681,'userid' => 4987463,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 30,'createtime' => '2020-02-18 17:50:28',),4 =>array('id' => 686,'userid' => 4987463,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 30,'createtime' => '2020-02-18 21:13:17',),5 =>array('id' => 689,'userid' => 4987463,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 30,'createtime' => '2020-02-19 00:10:00',),6 =>array('id' => 694,'userid' => 5026514,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 5,'createtime' => '2020-02-19 07:46:00',),7 =>array('id' => 698,'userid' => 5061307,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 5,'createtime' => '2020-02-19 12:42:48',),8 =>array('id' => 699,'userid' => 5061307,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 5,'createtime' => '2020-02-19 12:44:40',),9 =>array('id' => 700,'userid' => 5061307,'activity_id' => 27,'from_type' => 1,'type' => 127,'award_type' => 1,'award_value' => 10,'createtime' => '2020-02-19 12:45:40',),
);
for ($j &#61; 0; $j < 7; $j&#43;&#43;) {$arr &#61; array_merge($arr, $arr);
}echo count($arr) . &#39;个元素&#39; . PHP_EOL;function encode($arr)
{$s1 &#61; json_encode($arr);$s2 &#61; serialize($arr);$s3 &#61; swoole_pack($arr);$s4 &#61; msgpack_pack($arr);$l1 &#61; strlen($s1);$l2 &#61; strlen($s2);$l3 &#61; strlen($s3);$l4 &#61; strlen($s4);echo "字节长度&#xff1a;" . PHP_EOL;echo "1、json: {$l1}" . PHP_EOL;echo "2、serialize: {$l2}" . PHP_EOL;echo "3、swoole_pack: {$l3}" . PHP_EOL;echo "4、msgpack_pack: {$l4}" . PHP_EOL . PHP_EOL;$max &#61; 5000;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {json_encode($arr);}$end &#61; microtime(true);$t1 &#61; $end - $start;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {serialize($arr);}$end &#61; microtime(true);$t2 &#61; $end - $start;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {swoole_pack($arr);}$end &#61; microtime(true);$t3 &#61; $end - $start;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {msgpack_pack($arr);}$end &#61; microtime(true);$t4 &#61; $end - $start;echo "序列化耗时&#xff1a;" . PHP_EOL;echo "1、json: {$t1}" . PHP_EOL;echo "2、serialize: {$t2}" . PHP_EOL;echo "3、swoole_pack: {$t3}" . PHP_EOL;echo "4、msgpack_pack: {$t4}" . PHP_EOL . PHP_EOL;// ------------------------------------------------------------------------$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {json_decode($s1, true);}$end &#61; microtime(true);$t1 &#61; $end - $start;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {unserialize($s2);}$end &#61; microtime(true);$t2 &#61; $end - $start;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {swoole_unpack($s3);}$end &#61; microtime(true);$t3 &#61; $end - $start;$start &#61; microtime(true);for ($i &#61; 0; $i < $max; $i&#43;&#43;) {msgpack_unpack($s4);}$end &#61; microtime(true);$t4 &#61; $end - $start;echo "反序列化耗时&#xff1a;" . PHP_EOL;echo "1、json: {$t1}" . PHP_EOL;echo "2、serialize: {$t2}" . PHP_EOL;echo "3、swoole_pack: {$t3}" . PHP_EOL;echo "4、msgpack_pack: {$t4}" . PHP_EOL . PHP_EOL;
}encode($arr);

打印结果
在这里插入图片描述
由此可见&#xff1a;

  • swoole_pack 可将数据压缩的最小&#xff0c;并且耗时最小&#xff0c;但只能用在PHP语言&#xff1b;
  • msgpack_pack 耗时和 serialize 差不多&#xff0c;但是数据压缩的更小&#xff0c;并且多语言通用协议&#xff1b;
  • 如果数据存储在redis&#xff0c;那么数据压缩比例是个很重要的指标&#xff1b;
  • serialize 和 json 相比&#xff0c;占用存储空间大&#xff0c;但耗时缩短了一倍&#xff1b;
  • 很明显反序列化的耗时是序列化的3倍左右&#xff1b;

推荐阅读
author-avatar
杭州琦琦妈_120
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有