Data length (print_r): 610Kb
Serialize: 0.22sec. (472Kb)
Unserialize: 0.17sec.
JSON encode: 0.19sec. (303Kb)
JSON decode: 0.26sec.
<?
$ 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 .
Source: https://habr.com/ru/post/30210/
All Articles