Home Backend Development PHP Tutorial 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

Jul 21, 2016 pm 03:37 PM
decode encode json php serialize unserialize and point performance test of

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:
<?php
$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 of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles