php中serialize序列化与json性能测试的示例分析
最近需要对大数组做存储,需要在serialize序列化和json之间做了选择。因此需要做了性能测试。
在php5.2之前对数组存储的时候,大都使用serialize系列化。php5.2之后,开始内置了 JSON 的支持。
在网上看到有些资料说:json_encode和json_decode比内置的serialize和unserialize函数要高效。耳闻不如眼见,眼见不一定为实。那就用实际数据测试吧.....
我们先理解概念:
一、 序列化
序列化是将对象状态转换为可保持或可传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
将对象的状态信息转换为可以存储或传输的窗体的过程。 在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
通常,对象实例的所有字段都会被序列化,这意味着数据会被表示为实例的序列化数据。这样,能够解释该格式的代码有可能能够确定这些数据的值,而不依赖于该成员的可访问性。类似地,反序列化从序列化的表示形式中提取数据,并直接设置对象状态,这也与可访问性规则无关。 对于任何可能包含重要的安全性数据的对象,如果可能,应该使该对象不可序列化。如果它必须为可序列化的,请尝试生成特定字段来保存不可序列化的重要数据。如果无法实现这一点,则应注意该数据会被公开给任何拥有序列化权限的代码,并确保不让任何恶意代码获得该权限。
二、 JSON
JSON,JavaScript Object Notation,一种更轻、更友好的用于接口(AJAX、REST等)数据交换的格式。 JSON是结构化数据串行化的文本格式,作为XML的一种替代品,用于表示客户端与服务器间数据交换有效负载的格式。它是从ECMAScript语言标准衍生而来的。JSON的设计目标是使它成为小的、轻便的、文本的,而且是JavaScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
JSON建构有两种结构:
1. “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
2.值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
三、 实际测试
在PHP 5.3下执行:我们先使用小的数据做测试: 复制代码 代码如下:
$target = array (
'battle_id'=> 257
,'user_id'=> 41248
,'user_id2'=> 23989
,'player'=> 41248
,'formation'=> Array
(
'41248'=> 1
,'23989'=> 2
)
,'result'=> 1
,'battle_type'=> 1
,'speed'=> Array
(
'41248'=> 0
,'23989'=> 0
)
);
$json = json_encode($target);
$seri = serialize($target);
echo "json :" , strlen($json) ,'
';
echo "serialize :", strlen($seri) ,'
';
$stime = microtime(true);
for ($i = 0; $i json_encode($target);
}
$etime = microtime(true);
echo "json_encode :", ($etime - $stime) ,'
';
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i json_decode($json,true);
}
$etime = microtime(true);
echo "json_decode :", ($etime - $stime),'
';
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i serialize($target);
}
$etime = microtime(true);
echo "serialize :", ($etime - $stime) ,'
';
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i unserialize($seri);
}
$etime = microtime(true);
echo "unserialize :", ($etime - $stime),'
';
?>
测试结果:
json :156
serialize :222
json_encode :0.1087498664856
json_decode :0.12652111053467
serialize :0.041656017303467
unserialize :0.040987968444824
测试结果看出json效率稍微比serialize差点,在php5.2可能会更差。应该是在php5.3之后,json扩展做了优化。
然后使用大数组做测试(代码放到最后,因为代码的数组比较长):
测试结果:
json :5350
serialize :8590
json_encode :0.90479207038879
json_decode :1.753741979599
serialize :1.3566699028015
unserialize :1.3003630638123
我们可以看出,serialize比json差了快一个数量级。
总结:
1) 空间的比较
serialize在编码后大概是json的1.5倍。
原因:
- serialize后字符串包含了子串的长度,这可能是速度方面的优化,但是测试结果不尽人意。
- serialize有更加详细的类型区分,而json只有四种类型,并且是以简单的符号表示。
- 2)速度的比较
- 在较小数据的情况下, serialize比json快数量级。
在大数据量的情况下,json比serialize稍微差一点
3)处理对象
json无法处理对象方法等数据。4)使用范围
- 序列化使用serialize,特别是对象的存储。这是其存在的意义。
- 与对象无关的数据存储可以使用json,如包含大量数字的数组等。
-
- 在前后端交互一般都使用JSON,另外,目前JSON只支持UTF-8编码的数据。
复制代码 代码如下:
$target = array (
'battle_id'=> 257
,'user_id'=> 41248
,'user_id2'=> 23989
,'player'=> 41248
,'formation'=> Array ('41248'=> 1 ,'23989'=> 2)
,'result'=> 1
,'battle_type'=> 1
,'speed'=> Array( '41248'=> 0,'23989'=> 0 )
,'attacker'=> Array(
'1'=> Array (
'user_id'=> 41248
,'soldier_id'=> 28
,'prototype_id'=> 4
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3997
,'hp'=> 3997
,'attack_general'=> 346
,'attack_skill'=> 596
,'attack_explode'=> 458
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 2
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'4'=> Array (
'user_id'=> 41248
,'soldier_id'=> 29
,'prototype_id'=> 2
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3555
,'hp'=> 3555
,'attack_general'=> 396
,'attack_skill'=> 581
,'attack_explode'=> 418
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0
)
,'5'=> Array (
'user_id'=> 41248
,'soldier_id'=> 30
,'prototype_id'=> 6
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3043
,'hp'=> 3043
,'attack_general'=> 351
,'attack_skill'=> 540
,'attack_explode'=> 474
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'7'=> Array (
'user_id'=> 41248
,'soldier_id'=> 37
,'prototype_id'=> 2
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3491
,'hp'=> 3491
,'attack_general'=> 393
,'attack_skill'=> 532
,'attack_explode'=> 456
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0 ))
,'defender'=> Array(
'2'=> Array(
'user_id'=> 23989
,'soldier_id'=> 24
,'prototype_id'=> 1
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3230
,'hp'=> 3230
,'attack_general'=> 390
,'attack_skill'=> 567
,'attack_explode'=> 442
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'5'=> Array(
'user_id'=> 23989
,'soldier_id'=> 25
,'prototype_id'=> 2
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3400
,'hp'=> 3400
,'attack_general'=> 379
,'attack_skill'=> 536
,'attack_explode'=> 405
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0 )
,'7'=> Array(
'user_id'=> 23989
,'soldier_id'=> 26
,'prototype_id'=> 6
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3669
,'hp'=> 3669
,'attack_general'=> 362
,'attack_skill'=> 549
,'attack_explode'=> 426
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0 )
,'9'=> Array(
'user_id'=> 23989
,'soldier_id'=> 27
,'prototype_id'=> 1
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3618
,'hp'=> 3618
,'attack_general'=> 326
,'attack_skill'=> 510
,'attack_explode'=> 419
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0) )
,'battle_process'=> Array(
'0'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'1'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'2'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'3'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'4'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'5'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'6'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'7'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'8'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'9'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'10'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'11'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'12'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> Array( '0'=> 26 )
,'harm'=> Array('0'=> 1650)
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0

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

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
