Home  >  Article  >  Backend Development  >  Performance test analysis of json_encode, json_decode, serialize, and unserialize in PHP_PHP Tutorial

Performance test analysis of json_encode, json_decode, serialize, and unserialize in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:37:13882browse

So I thought about how to serialize and store objects in PHP in the most cost-effective way? Then I thought of the JSON encoding and decoding functions recommended by my previous colleagues.
According to him, json_encode and json_decode are more efficient than the built-in serialize and unserialize functions.
So I decided to conduct an experiment to confirm whether what my colleagues said was true.
The experiments were conducted in PHP 5.2.13 and PHP 5.3.2 environments respectively.
Use the same variable to encode or decode 10,000 times using the above method, and get the time required to execute each function 10,000 times.
The following is one of the test results in the PHP 5.2.13 environment:

Copy code The code is as follows:

json : 190
serialize : 257
json_encode : 0.08364200592041
json_decode : 0.18004894256592
serialize : 0.063642024993896
unserialize : 0.086990833 282471
DONE.

The following is PHP 5.3.2 One of the test results of the environment:
Copy code The code is as follows:

json: 190
serialize: 257
json_encode : 0.062805891036987
json_decode : 0.14239192008972
serialize : 0.048481941223145
unserialize : 0.05927300453186
DONE. >
The conclusion from this experiment is:
json_encode and json_decode The efficiency is not higher than that of serialize and unserialize. The performance difference during deserialization is about twice. The execution efficiency of PHP 5.3 is slightly improved than that of PHP 5.2.
The following is the code I used for testing:

Copy the code The code is as follows:
$target = array (
'name' => 'Almighty Helmet',
'quality' => 'Blue',
'ti_id' => 21302,
'is_bind ' => 1,
'demand_conditions' =>
array (
'HeroLevel' => 1,
),
'quality_attr_sign' =>
'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.';
?>


http://www.bkjia.com/PHPjc/321975.html

truehttp: //www.bkjia.com/PHPjc/321975.htmlTechArticleSo I thought of how to serialize and store objects in PHP in the most cost-effective way? Then I thought of the JSON encoding and decoding functions recommended by my previous colleagues. According to him, json_encode and json_deco...
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