📜 ⬆️ ⬇️

Coding and decoding performance of serialize and json

The idea arose to compare two ways of storing abstract data in the form of strings implemented in PHP: the good old serialization and the relatively recent json format that supports the language.

For comparison, a small script generates an array of 1000 elements, two thirds of the elements of which are strings, 15 characters long (half letters and only numbers), and the remaining third are arrays of 50 elements (also alphanumeric and half elements).

The result is very ambiguous, the details in the second part .
')


At the same time, I made sure that serialize / unserialize and json_encode / json_decode are very fast functions and hardly capable of becoming a bottleneck with reasonable data volumes.

Encoding and decoding was performed 10 times for each method, as the result of the work the script gives the total time of all 10 operations.

Visual chart (at the same time I learned the basics of Google Chart API):

serialize vs json

The result of the script:

Data length (print_r): 610Kb
Serialize: 0.22sec. (472Kb)
Unserialize: 0.17sec.
JSON encode: 0.19sec. (303Kb)
JSON decode: 0.26sec.


Script code used for testing:

<?
$ data = array ();

// digits
for ($ i = 0; $ i <500; $ i ++)
{
$ data [] = getRandomString ( true , 15);
}

// small letters
for ($ i = 0; $ i <500; $ i ++)
{
$ data [] = getRandomString ( false , 15);
}

// nesting one third of elements have nesting
for ($ i = 0; $ i <334; $ i ++)
{
$ index = intval (rand (0, 1000));
$ data [$ index] = array ();

// nesting of 50 elements
for ($ j = 0; $ j <25; $ j ++)
{
$ data [$ index] [] = getRandomString ( false , 15);
$ data [$ index] [] = getRandomString ( true , 15);
}

// shuffle
shuffle ($ data [$ index]);
}

// shuffle
shuffle ($ data);

$ rounds = 10;

// serialize

echo 'Data length (print_r):' . round (strlen (print_r ($ data, true )) / 1024). 'Kb <br/>' ;

//coding
$ a = new pgt;
for ($ i = 0; $ i <$ rounds; $ i ++)
{
$ serialize = serialize ($ data);
}
$ d [0] = round ($ a-> getValue (), 2);
echo 'Serialize:' . $ d [0]. 'sec. (' . round (strlen ($ serialize) / 1024). ' Kb) <br/> ' ;

//decoding
$ a = new pgt;
for ($ i = 0; $ i <$ rounds; $ i ++)
{
$ serialize1 = unserialize ($ serialize);
}
$ d [1] = round ($ a-> getValue (), 2);
echo 'Unserialize:' . $ d [1]. 'sec. <br/>' ;

// json

//coding
$ a = new pgt;
for ($ i = 0; $ i <$ rounds; $ i ++)
{
$ json = json_encode ($ data);
}
$ d [2] = round ($ a-> getValue (), 2);
echo 'JSON encode:' . $ d [2]. 'sec. (' . round (strlen ($ json) / 1024). ' Kb) <br/> ' ;

//decoding
$ a = new pgt;
for ($ i = 0; $ i <$ rounds; $ i ++)
{
$ json1 = json_decode ($ json);
}
$ d [3] = round ($ a-> getValue (), 2);
echo 'JSON decode:' . $ d [3]. 'sec. <br/>' ;

foreach ($ d as $ index => $ value )
{
$ d [$ index] = $ d [$ index] * 100 * 3.7;
}

// $ val = 'http://chart.apis.google.com/chart?cht=bvg&chd=t:'. $ d [0]. ','. $ d [2]. '|'. $ d [1]. ','. $ d [3]. '& chs = 260x300 & chl = serialize | json & chco = 4d89f9, c6d9fd & chdl = encoding | decoding & chxt = y & chxl = 0: | 0 sec | 0.027 sec';
// echo '<br/> <img src = "'. $ val. '" />';

class pgt {

function __construct () {
$ this -> time = $ this -> GetTime ();
}

public function getValue () {
$ value = $ this -> GetTime () - $ this -> time;
return $ value ;
}

// service interior
private function getTime () {
list ($ usec, $ sec) = explode ( "" , microtime ());
return (( float ) $ usec + ( float ) $ sec);
}

}

function getRandomString ($ letters, $ length)
{
if ($ letters === true )
{
$ a = 'ABCDEFZHIJKLMKOPQRSTUVWXYZabcdefzhijklmkopqrstuvwxyz' ;
}
else
{
$ a = '0123456789' ;
}

$ result = '' ;

for ($ i = 0; $ i <$ length; $ i ++)
{
$ result. = substr ($ a, intval (rand (0, strlen ($ a) - 1)), 1);
}

return $ result;
}
?> * This source code was highlighted with Source Code Highlighter .


PS My first post on Habré :-)

Update: thanks for the constructive comments, they were the impetus for the second part of the material.

Source: https://habr.com/ru/post/30210/


All Articles