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

对比以下四个方法

1、json_encode
序列化后可阅读,多语言通用协议。

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

PHP版本 7.2.27

安装扩展

pecl install swoole_serialize-0.1.1
pecl install msgpack

extension=msgpack
extension=swoole_serialize

php --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 = 0; $j < 7; $j++) {
    $arr = array_merge($arr, $arr);
}

echo count($arr) . '个元素' . PHP_EOL;

function encode($arr)
{
    $s1 = json_encode($arr);
    $s2 = serialize($arr);
    $s3 = swoole_pack($arr);
    $s4 = msgpack_pack($arr);

    $l1 = strlen($s1);
    $l2 = strlen($s2);
    $l3 = strlen($s3);
    $l4 = strlen($s4);

    echo "字节长度:" . 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 = 5000;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        json_encode($arr);
    }
    $end = microtime(true);
    $t1  = $end - $start;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        serialize($arr);
    }
    $end = microtime(true);
    $t2  = $end - $start;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        swoole_pack($arr);
    }
    $end = microtime(true);
    $t3  = $end - $start;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        msgpack_pack($arr);
    }
    $end = microtime(true);
    $t4  = $end - $start;

    echo "序列化耗时:" . 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 = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        json_decode($s1, true);
    }
    $end = microtime(true);
    $t1  = $end - $start;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        unserialize($s2);
    }
    $end = microtime(true);
    $t2  = $end - $start;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        swoole_unpack($s3);
    }
    $end = microtime(true);
    $t3  = $end - $start;

    $start = microtime(true);
    for ($i = 0; $i < $max; $i++) {
        msgpack_unpack($s4);
    }
    $end = microtime(true);
    $t4  = $end - $start;

    echo "反序列化耗时:" . 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);

打印结果
在这里插入图片描述
由此可见:

  • swoole_pack 可将数据压缩的最小,并且耗时最小,但只能用在PHP语言;
  • msgpack_pack 耗时和 serialize 差不多,但是数据压缩的更小,并且多语言通用协议;
  • 如果数据存储在redis,那么数据压缩比例是个很重要的指标;
  • serialize 和 json 相比,占用存储空间大,但耗时缩短了一倍;
  • 很明显反序列化的耗时是序列化的3倍左右;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值