PHP中serializen()与json_encode()的性能差异

发布时间:2017年08月25日 // 分类:代码 // 1 条评论

结论

serializen的效率远高于json,特别是在反序列化方面。

实地测试

对同一个巨大数组分别进行10000次序列化和反序列化,得到如下测试结果:

测试环境
CPU: Intel i5-3317U

PHP: 7.0.12
SRV: Apache 2

json_encode    2.7458431720734 s
json_decode    7.1210720539093 s
serialize    1.899829864502 s
unserialize    0.033617973327637 s

测试代码

<?php

       $de=[
            "ok"=>23421344234,
            "d3"=>['32af','23rdsf','fa3fawe','r23dsds'],
            'asdfasfd'=>['sadfasdf','asdfdsf'=>'asdfa爱的色放撒地方发生但凡sfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfasfd1'=>['sadfa安抚仨人货币补偿现在vcxzvcxzsdf','asg发多少了空间dfdsf'=>'asdfasfdsdfsfa','asdf爱居兔肉色很尴尬人个人sadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfasfd2'=>['sadfasdf','asdfdsf'=>'asdfasfds阿斯顿发生dfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfasfd3'=>['sadf爱迪生范德萨发放第三asdf','asdfdsf'=>'asdfasfd阿萨德发的顺丰sdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfasfd4'=>['sadfasdf','asdfdsf'=>'asdfasfdsd阿什顿发发生的fsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfahgewrasfd'=>['sadfasdf','asdfdsf'=>'asdfa阿什顿发斯蒂芬sfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdffsadasfd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdaefdssfasfd'=>['sadfasdf','asdf阿斯顿发发生的dsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfa阿斯顿发发呆爽肤水对方43wefrasfd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'as阿斯蒂芬范德萨范德萨dfsdaf']],
            'asdfaasdfsfd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'afasdfsdfasfd'=>['sadfasdf','asdfdsf'=>'asdfas阿斯顿发发生的分fdsdfsfa','as爱的色放范德萨dfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            'asdfas阿什顿发斯蒂芬法第三asdfsdffd'=>['sadfasdf','asdfdsf'=>'asdfasfdsdfsfa','asdfsadf'=>['asdfasdf',234234234,'asdfsdaf']],
            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    
                     )    
                     )
        ];
        $de=[
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
            $de,
        ];

        $stime = microtime(true);    
        for ($i = 0; $i < 10000; $i ++) {    
            $en=json_encode($de);    
        }    
        $etime = microtime(true);   
        echo "json_encode :", ($etime - $stime) ,'<br/>';    



        $stime = microtime(true);    
        for ($i = 0; $i < 10000; $i ++) {    
            $de=json_decode($en);    
        }    
        $etime = microtime(true);    
        echo "json_decode :", ($etime - $stime) ,'<br/>';    


        $stime = microtime(true);    
        for ($i = 0; $i < 10000; $i ++) {    
            $en=serialize($de);    
        }    
        $etime = microtime(true);    
        echo "serialize  :", ($etime - $stime) ,'<br/>';    


        $stime = microtime(true);    
        for ($i = 0; $i < 10000; $i ++) {    
            $de=serialize($en);    
        }    
        $etime = microtime(true);    
        echo "unserialize  :", ($etime - $stime) ,'<br/>';    

本文固定链接
https://www.ywlib.com/archives/119.html

标签
php, serializen, json

仅有一条 关于 " PHP中serializen()与json_encode()的性能差异 "的评论.

  1. 1

    代码都写错了,serialize 写了两次

添加新评论 »

分类
随机文章
最新文章
最近回复
  • 1: 代码都写错了,serialize 写了两次
  • ANTONIO: 谢谢你, 终于有声音了!!!
  • andyw: newbee的版主!好用
  • amos: 真管用!!!感谢版主
  • Kent: 优秀!
  • Kent: 关于页面里有我的邮箱
  • kincae: 你好,找了很多文档就你的这篇解决了我的问题,可以留个联系方式吗
  • xuesheng: phpredis版本的问题。 phpredis 5.3.0 fixed. * Use long...
  • Proxmox VE 6.1 配置源及关闭订阅提醒 - ZIMRI`Blog: [...]原文链接 https://www.ywlib.com/archives/150.ht...
  • 夏目贵志: 感谢解决困惑我2小时的问题!!!