Performance test analysis of json_encode json_decode serialize and unserialize in PHP
- 2020-03-31 20:54:26
- OfStack
So I thought, how do you serialize an object in PHP and store it for the best value? Then I thought of the JSON encoding and decoding functions recommended by my colleagues.
According to him, json_encode and json_decode are more efficient than the built-in serialize and unserialize functions.
So I decided to experiment to see if what my colleague said was true.
The experiment was carried out in the environment of PHP 5.2.13 and PHP 5.3.2, respectively.
The same variable is used to encode or decode 10000 times in the above way respectively, and the time required for each function to execute 10000 times is obtained.
Here are the results of one of the tests in the PHP 5.2.13 environment:
Here are the results of one of the tests in the PHP 5.3.2 environment:
The experiment concluded that:
Json_encode and json_decode are no more efficient than serialize and unserialize, with about twice the performance difference when deserializing, and PHP 5.3 performs slightly better than PHP 5.2.
Here is the code I used to do the test:
According to him, json_encode and json_decode are more efficient than the built-in serialize and unserialize functions.
So I decided to experiment to see if what my colleague said was true.
The experiment was carried out in the environment of PHP 5.2.13 and PHP 5.3.2, respectively.
The same variable is used to encode or decode 10000 times in the above way respectively, and the time required for each function to execute 10000 times is obtained.
Here are the results of one of the tests in the PHP 5.2.13 environment:
json : 190
serialize : 257
json_encode : 0.08364200592041
json_decode : 0.18004894256592
serialize : 0.063642024993896
unserialize : 0.086990833282471
DONE.
Here are the results of one of the tests in the PHP 5.3.2 environment:
json : 190
serialize : 257
json_encode : 0.062805891036987
json_decode : 0.14239192008972
serialize : 0.048481941223145
unserialize : 0.05927300453186
DONE.
The experiment concluded that:
Json_encode and json_decode are no more efficient than serialize and unserialize, with about twice the performance difference when deserializing, and PHP 5.3 performs slightly better than PHP 5.2.
Here is the code I used to do the test:
<?php
$target = array (
'name' => ' Universal helmet ',
'quality' => 'Blue',
'ti_id' => 21302,
'is_bind' => 1,
'demand_conditions' =>
array (
'HeroLevel' => 1,
),
'quality_attr_sign' =>
array (
'HeroStrength' => 8,
'HeroAgility' => 8,
'HeroIntelligence' => 8,
),
);
$json = json_encode($target);
$seri = serialize($target);
echo "json :tt" . strlen($json) . "rn";
echo "serialize :t" . strlen($seri) . "rnrn";
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_encode($target);
}
$etime = microtime(true);
echo "json_encode :t" . ($etime - $stime) . "rn";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_decode($json);
}
$etime = microtime(true);
echo "json_decode :t" . ($etime - $stime) . "rnrn";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
serialize($target);
}
$etime = microtime(true);
echo "serialize :t" . ($etime - $stime) . "rn";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
unserialize($seri);
}
$etime = microtime(true);
echo "unserialize :t" . ($etime - $stime) . "rnrn";
echo 'DONE.';
?>