LEN

PHP 序列化选择
今天接手一个新项目 codis监控管理读了一天的代码, 发现数组转存到redis中序列化采用的是serialize...
扫描右侧二维码阅读全文
18
2017/05

PHP 序列化选择

今天接手一个新项目 codis监控管理
读了一天的代码, 发现数组转存到redis中序列化采用的是serialize
总结了下 php 的序列化

  1. json_encode
  2. serialize
  3. base64_encode

也就上面三种常用序列化方式了. 当然还有pack.这个暂时没玩明白
本文主要比较json_encode 和 serialize 两个, base64_encode 编码后字节数据太大,如果是数据传输保证数据一致性,我会用base64_encode
从网上找个代码:

        <?php
        /*
        * File Name: test.php
        * Author: Len
        * mail: i@91coder.org
        * Created Time: Wed 17 May 2017 10:50:17 PM CST
        * 
        */
        $target = array(
            'battle_id' => 257,
            'user_id' => 41248,
            'user_id2' => 23989,
            'player' => 41248,
            'formation' => Array
            (
                '41248' => 1,
                '23989' => 2
            ),
            'result' => 1,
            'battle_type' => 1,
            'speed' => Array
            (
                '41248' => 0,
                '23989' => 0
            )
        );

        $json = json_encode($target);
        $seri = serialize($target);

        echo "json :", strlen($json), PHP_EOL;
        echo "serialize :", strlen($seri), PHP_EOL;

        $stime = microtime(true);
        for ($i = 0; $i < 10000; $i++) {
            json_encode($target);
        }
        $etime = microtime(true);

        echo "json_encode :", ($etime - $stime), PHP_EOL;

        $stime = microtime(true);
        for ($i = 0; $i < 10000; $i++) {
            json_decode($json, true);
        }
        $etime = microtime(true);

        echo "json_decode :", ($etime - $stime), PHP_EOL;

        $stime = microtime(true);
        for ($i = 0; $i < 10000; $i++) {
            serialize($target);
        }
        $etime = microtime(true);

        echo "serialize :", ($etime - $stime), PHP_EOL;

        $stime = microtime(true);
        for ($i = 0; $i < 10000; $i++) {
            unserialize($seri);
        }
        $etime = microtime(true);

        echo "unserialize :", ($etime - $stime), PHP_EOL;

PHP5.5 执行结果

[root@bf ~]#php -v
PHP 5.5.23 (cli) (built: Nov 23 2015 15:12:07) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
[root@bf ~]#php test.php 
json :156
serialize :222
json_encode :0.018645048141479
json_decode :0.041597843170166
serialize :0.022917985916138
unserialize :0.024193048477173

PHP7 执行结果

                                               
[root@localhost question_game_admin]# /usr/local/php7/bin/php -v
PHP 7.0.3 (cli) (built: Oct 14 2016 21:38:26) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
[root@localhost question_game_admin]# /usr/local/php7/bin/php test1.php 
json :156
serialize :222
json_encode :0.0072259902954102
json_decode :0.029087066650391
serialize :0.0057480335235596
unserialize :0.012353897094727

总结执行结果是 json_encode 性能上明显高出serialize 一个量级, 而且json格式可读性很强!

而且serialize 格式化汉字字符标点等会存在一些问题, 在这里我推荐使用json_encode进行格式化

最后修改:2017 年 05 月 18 日 01 : 17 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论