Sample analysis of serialize serialization and json performance testing in php

  • 2020-06-01 08:41:10
  • OfStack

Recently, we needed to store a large array, so we had to choose between serialize serialization and json serialization. So you need to do performance testing.

When storing arrays before php 5.2, serialize serialization is mostly used. After php 5.2, JSON support was built in.

I read some information on the Internet that says json_encode and json_decode are more efficient than the built-in serialize and unserialize functions. Seeing is better than hearing. Seeing is believing. Let's test it with real data...

Let's understand the concept first:

1. The serialization

Serialization is the process of converting the state of an object into a format that can be maintained or transferred. The opposite of serialization is deserialization, which converts streams into objects. Together, these two processes make it easy to store and transfer data.

The process of converting the state information of an object into a form that can be stored or transferred. During serialization, an object writes its current state to a temporary or persistent store. Later, you can recreate the object by reading or deserializing its state from the store.

Typically, all fields of an object instance are serialized, which means that the data is represented as serialized data for the instance. This makes it possible for code that interprets the format to determine the value of the data, independent of the accessibility of the member. Similarly, deserialization extracts data from the serialized representation and sets the object state directly, again irrespective of accessibility rules. For any object that might contain significant security data, make it unserializable if possible. If it must be serializable, try generating specific fields to hold important data that is not serializable. If this point cannot be achieved, be aware that the data is exposed to any code that has serialization permission, and make sure that no malicious code gets that permission.


2. JSON

JSON, JavaScript Object Notation, a lighter, friendlier format for data exchange between interfaces (AJAX, REST, etc.). JSON is a text format for structured data serialization, an alternative to XML, used to represent the data exchange payload between the client and the server. It is derived from the ECMAScript language standard. The JSON is designed to be small, lightweight, textual, and a subset of JavaScript. JSON USES a completely language-independent text format, but also USES habits similar to those of the C language family (C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. Easy for people to read and write, but also easy for machines to parse and generate.

JSON is constructed in two ways:
1. Collection of name/value pairs (A collection of name/value pairs) In different languages, it is understood as an object (object), a record (record), a structure (struct), a dictionary (dictionary), a hash table (hash table), a list of keys (keyed list), or an associative array (associative array).


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

Testing:

3. Actual testing

Execute under PHP 5.3: we first use small data for testing:


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

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

    echo "json_encode :", ($etime - $stime) ,'<br/>';    

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

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

    echo "json_decode :", ($etime - $stime),'<br/>';    

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

    echo "serialize :", ($etime - $stime) ,'<br/>';    

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

    echo "unserialize :", ($etime - $stime),'<br/>';    

    ?>    

Test results:

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

The test results show that the efficiency of json is slightly worse than that of serialize, and it may be worse in php 5.2. The json extension was optimized after php 5.3.

Then use a large array to do the test (save the code at the end because the array of the code is long) :

Test results:

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

We can see that serialize is one order of magnitude faster than json.

Conclusion:

1) spatial comparison

serialize is about 1.5 times larger than json after encoding.

The reason:

The string after serialize contains the length of the substring, which may be an optimization for speed, but the test results are unsatisfactory. While serialize has a more detailed type distinction, json has only four types and is represented in simple symbols. 2) comparison of speed For smaller Numbers, serialize is orders of magnitude faster than json.

In the case of large data, json is slightly worse than serialize by 1 point

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

4) scope of use

Serialization USES serialize, especially for object storage. That's why it exists. You can use json for object-independent data stores, such as arrays containing large Numbers. JSON is generally used in front and rear end interactions 1. In addition, currently JSON only supports UTF-8 encoded data.


 <?php  

$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'=> 0  
                   ,'state'=> 0  
                )  
           ,'13'=> 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  
                )  
           ,'14'=> 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  
                )  

           ,'15'=> 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  
                )  
           ,'16'=> 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  
                )  
           ,'17'=> 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  
                )  

           ,'18'=> 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  
                )  
           ,'19'=> 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  
                )  

           ,'20'=>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  
                )  
        )  

);  

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

echo "json :" , strlen($json) ,'<br/>';  
echo "serialize :", strlen($seri) ,'<br/>';  

$stime = microtime(true);  
for ($i = 0; $i < 10000; $i ++) {  

                

Related articles: