search
HomeBackend DevelopmentPHP TutorialExample analysis of serialize serialization and json performance testing in php_PHP tutorial
Example analysis of serialize serialization and json performance testing in php_PHP tutorialJul 21, 2016 pm 03:11 PM
jsonphpserializeandDoanalyzestorageSerializationperformancearraytestofExampleneed

最近需要对大数组做存储,需要在serialize序列化和json之间做了选择。因此需要做了性能测试。

在php5.2之前对数组存储的时候,大都使用serialize系列化。php5.2之后,开始内置了 JSON 的支持

在网上看到有些资料说:json_encode和json_decode比内置的serialize和unserialize函数要高效。耳闻不如眼见,眼见不一定为实。那就用实际数据测试吧.....

我们先理解概念:

一、 序列化

序列化是将对象状态转换为可保持或可传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。

将对象的状态信息转换为可以存储或传输的窗体的过程。 在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。

通常,对象实例的所有字段都会被序列化,这意味着数据会被表示为实例的序列化数据。这样,能够解释该格式的代码有可能能够确定这些数据的值,而不依赖于该成员的可访问性。类似地,反序列化从序列化的表示形式中提取数据,并直接设置对象状态,这也与可访问性规则无关。 对于任何可能包含重要的安全性数据的对象,如果可能,应该使该对象不可序列化。如果它必须为可序列化的,请尝试生成特定字段来保存不可序列化的重要数据。如果无法实现这一点,则应注意该数据会被公开给任何拥有序列化权限的代码,并确保不让任何恶意代码获得该权限。


二、 JSON

JSON, JavaScript Object Notation, a lighter and more friendly format for data exchange through interfaces (AJAX, REST, etc.). JSON is a text format for serializing structured data. As an alternative to XML, it is used to represent the payload of data exchange between clients and servers. It is derived from the ECMAScript language standard. JSON's design goals are to make it small, lightweight, textual, and a subset of JavaScript. JSON adopts a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language. Easy for humans to read and write, and easy for machines to parse and generate.

JSON construction has two structures:
1. A collection of name/value pairs . In different languages, it is understood as an object, a record, a struct, a dictionary, a hash table, a keyed list, or an associative array. array).


2. An ordered list of values. In most languages, it is understood as an array.

Test:

3. Actual Test

Executed under PHP 5.3: We first use small data for testing:

Copy the code The code is as follows:

        $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) ,'
';   
    echo "serialize :", strlen($seri) ,'
';   

    $stime = microtime(true);   
    for ($i = 0; $i         json_encode($target);   
    }   
    $etime = microtime(true);   

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

    //----------------------------------   

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

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

    //----------------------------------   
    $stime = microtime(true);   
    for ($i = 0; $i         serialize($target);   
    }   
    $etime = microtime(true);   

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

    //----------------------------------   
    $stime = microtime(true);   
    for ($i = 0; $i         unserialize($seri);   
    }   
    $etime = microtime(true);   

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

    ?>   

测试结果:

json :156
serialize :222
json_encode :0.1087498664856
json_decode :0.12652111053467
serialize :0.041656017303467
unserialize :0.040987968444824

测试结果看出json效率稍微比serialize差点,在php5.2可能会更差。应该是在php5.3之后,json扩展做了优化。

然后使用大数组做测试(代码放到最后,因为代码的数组比较长):

测试结果:

json :5350
serialize :8590
json_encode :0.90479207038879
json_decode :1.753741979599
serialize :1.3566699028015
unserialize :1.3003630638123

我们可以看出,serialize比json差了快一个数量级。

总结:

1) 空间的比较

serialize在编码后大概是json的1.5倍

Reason:

  • After serializing, the string contains the length of the substring. This may be a speed optimization, but the test results are unsatisfactory.
  • Serialize has more detailed type distinctions, while json only has four types, and they are represented by simple symbols.
  • 2) Comparison of speed
  • In the case of smaller data, serialize is orders of magnitude faster than json.

    In the case of large data volume, json is slightly worse than serialize

    3) Processing objects
    json cannot handle data such as object methods.

    4) Scope of use

    • Use serialize for serialization, especially for object storage. This is the meaning of its existence.
    • Object-independent data storage can use json, such as arrays containing large numbers, etc.
      • JSON is generally used for front-end and back-end interactions. In addition, Currently JSON only supports UTF-8 encoded data.

Copy code The code is as follows:

 
$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  ) 
   ,'attacker'=> Array( 
    '1'=> Array ( 
                   'user_id'=> 41248 
                   ,'soldier_id'=> 28 
                   ,'prototype_id'=> 4 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3997 
                   ,'hp'=> 3997 
                   ,'attack_general'=> 346 
                   ,'attack_skill'=> 596 
                   ,'attack_explode'=> 458 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 2 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0) 
           ,'4'=> Array ( 
                   'user_id'=> 41248 
                   ,'soldier_id'=> 29 
                   ,'prototype_id'=> 2 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3555 
                   ,'hp'=> 3555 
                   ,'attack_general'=> 396 
                   ,'attack_skill'=> 581 
                   ,'attack_explode'=> 418 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0 
                ) 
           ,'5'=> Array                ( 
                   'user_id'=> 41248 
                   ,'soldier_id'=> 30 
                   ,'prototype_id'=> 6 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3043 
                   ,'hp'=> 3043 
                   ,'attack_general'=> 351 
                   ,'attack_skill'=> 540 
                   ,'attack_explode'=> 474 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0) 
           ,'7'=> Array ( 
                   'user_id'=> 41248 
                   ,'soldier_id'=> 37 
                   ,'prototype_id'=> 2 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3491 
                   ,'hp'=> 3491 
                   ,'attack_general'=> 393 
                   ,'attack_skill'=> 532 
                   ,'attack_explode'=> 456 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0   )) 
   ,'defender'=> Array( 
           '2'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 24 
                   ,'prototype_id'=> 1 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3230 
                   ,'hp'=> 3230 
                   ,'attack_general'=> 390 
                   ,'attack_skill'=> 567 
                   ,'attack_explode'=> 442 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0) 
           ,'5'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 25 
                   ,'prototype_id'=> 2 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3400 
                   ,'hp'=> 3400 
                   ,'attack_general'=> 379 
                   ,'attack_skill'=> 536 
                   ,'attack_explode'=> 405 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0 ) 
           ,'7'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 26 
                   ,'prototype_id'=> 6 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3669 
                   ,'hp'=> 3669 
                   ,'attack_general'=> 362 
                   ,'attack_skill'=> 549 
                   ,'attack_explode'=> 426 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0 ) 
           ,'9'=> Array( 
                   'user_id'=> 23989 
                   ,'soldier_id'=> 27 
                   ,'prototype_id'=> 1 
                   ,'bid'=> 1 
                   ,'level'=> 1 
                   ,'rare'=> 1 
                   ,'skill_id'=> 1 
                   ,'totalhp'=> 3618 
                   ,'hp'=> 3618 
                   ,'attack_general'=> 326 
                   ,'attack_skill'=> 510 
                   ,'attack_explode'=> 419 
                   ,'attack_type'=> 1 
                   ,'defense'=> 0 
                   ,'anger'=> 50 
                   ,'dodge'=> 2 
                   ,'crit'=> 2 
                   ,'block'=> 0 
                   ,'block_effect'=> 0.5 
                   ,'crit_effect'=> 2 
                   ,'foramtion_effect'=> 0) ) 
   ,'battle_process'=> Array( 
           '0'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'1'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'2'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 
           ,'3'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'4'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 
           ,'5'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'6'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'7'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 
           ,'8'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'9'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 
           ,'10'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 
           ,'11'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=> 0 
                   ,'state'=> 0 
                ) 

           ,'12'=> Array( 
                   'user_id'=> 41248 
                   ,'asid'=> 28 
                   ,'bsid'=> Array( '0'=> 26 ) 
                   ,'harm'=> Array('0'=> 1650) 
                   ,'dhp'=> Array('0'=> 2019  ) 
                   ,'attacker_anger'=> 66 
                   ,'defender_anger'=> Array('0'=> 94 ) 
                   ,'skill'=&g
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),