52ll.org
52ll.org

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

测试代码

<?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) . PHP_EOL;

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

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

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

测试平台

-------------------- A Bench.sh Script By Teddysun -------------------
 Version            : v2022-06-01
 Usage              : wget -qO- bench.sh | bash
----------------------------------------------------------------------
 CPU Model          : Intel Core Processor (Skylake, IBRS)
 CPU Cores          : 1 @ 3792.000 MHz
 CPU Cache          : 16384 KB
 AES-NI             : Enabled
 VM-x/AMD-V         : Disabled
 Total Disk         : 30.0 GB (22.3 GB Used)
 Total Mem          : 976.8 MB (415.3 MB Used)
 Total Swap         : 2.9 GB (164.2 MB Used)
 System uptime      : 328 days, 15 hour 56 min
 Load average       : 0.00, 0.02, 0.00
 OS                 : Ubuntu 20.04.4 LTS
 Arch               : x86_64 (64 Bit)
 Kernel             : 5.4.0-99-generic
----------------------------------------------------------------------

php7版本

PHP 7.4.28 (cli) (built: Mar  7 2022 20:30:02) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

php8版本

PHP 8.1.3 (cli) (built: Jan 27 2023 12:07:44) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies

测试结果

php7

root@ubuntu:~# php /root/test.php
json_encode :0.73512196540833
json_decode :2.7060551643372
serialize  :0.66275596618652
unserialize  :0.014846086502075

php8

root@ubuntu:~# /usr/local/php8.1/bin/php /root/test.php
json_encode :0.81773495674133
json_decode :2.7036190032959
serialize  :0.49606585502625
unserialize  :0.013427972793579

测试结论

  • php8的json编码性能不如php7
  • php8的json解码性能与php7相当
  • 序列化反序列化的性能均好于json编码解码
  • php8的序列化性能好于php7

参考来源

https://www.ywlib.com/archives/119.html

发表回复

textsms
account_circle
email

52ll.org

PHP中serializen()与json_encode()的性能差异
测试代码 <?php $de = [ "ok" => 23421344234, "d3" => ['32af', '23rdsf', 'fa3fawe', 'r23dsds'], 'asdfasfd' => ['sadfasdf', 'asdfdsf' => 'asdfa爱…
扫描二维码继续阅读
2023-01-30