PHP中json_encode、json_decode与serialize、unserialize的性能测试分析
于是便联想到PHP中的对象怎么样序列化存储性价比最高呢?接着想到了之前同事推荐的JSON编码和解码函数。
据他所说,json_encode和json_decode比内置的serialize和unserialize函数要高效。
于是我决定动手实验,证实一下同事所说的情况是否属实。
实验分别在PHP 5.2.13和PHP 5.3.2环境下进行。
用同一个变量,分别用以上方式进行编码或解码10000次,并得出每个函数执行10000次所需的时间。
以下是PHP 5.2.13环境其中一次测试结果:
复制代码 代码如下:
json : 190
serialize : 257
json_encode : 0.08364200592041
json_decode : 0.18004894256592
serialize : 0.063642024993896
unserialize : 0.086990833282471
DONE.
以下是PHP 5.3.2环境其中一次测试结果:
复制代码 代码如下:
json : 190
serialize : 257
json_encode : 0.062805891036987
json_decode : 0.14239192008972
serialize : 0.048481941223145
unserialize : 0.05927300453186
DONE.
这次实验得到的结论是:
json_encode和json_decode的效率并没有比serialize和unserialize的效率高,在反序列化的时候性能相差两倍左右,PHP 5.3执行效率比PHP 5.2略有提升。
以下是我用来做测试的代码:
复制代码 代码如下:
$target = array (
'name' => '全能头盔',
'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 :\t\t" . strlen($json) . "\r\n";
echo "serialize :\t" . strlen($seri) . "\r\n\r\n";
$stime = microtime(true);
for ($i = 0; $i {
json_encode($target);
}
$etime = microtime(true);
echo "json_encode :\t" . ($etime - $stime) . "\r\n";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i {
json_decode($json);
}
$etime = microtime(true);
echo "json_decode :\t" . ($etime - $stime) . "\r\n\r\n";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i {
serialize($target);
}
$etime = microtime(true);
echo "serialize :\t" . ($etime - $stime) . "\r\n";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i {
unserialize($seri);
}
$etime = microtime(true);
echo "unserialize :\t" . ($etime - $stime) . "\r\n\r\n";
echo 'DONE.';
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
