Home php教程 php手册 PHP5.5四种序列化性能对比

PHP5.5四种序列化性能对比

Jun 06, 2016 pm 07:55 PM
json Compared Serialization performance

json_encode,serialize,igbinary,msgpack四种序列化方式,在之前已经有过相关的测试,PHP5.5这方面的测试暂时没有,这次测试基于PHP5.5,并且测试用例, http://blog.csdn.net/hguisu/article/details/7651730 的测试用例是一样的,只是从这个测试上家里i

json_encode,serialize,igbinary,msgpack四种序列化方式,在之前已经有过相关的测试,PHP5.5这方面的测试暂时没有,这次测试基于PHP5.5,并且测试用例,
http://blog.csdn.net/hguisu/article/details/7651730
的测试用例是一样的,只是从这个测试上家里igbinary serialize的测试,作为对比,可以参考
http://www.ooso.net/archives/538

运行环境
        PHP5.5 内存 16G 8核 2.0GMHz

性能&空间大小列表
PHP5.5四种序列化性能对比

采用小数组测试结果(注意为了数据好看,小数组测试时,循环次数为10000次,大数组为1000次)

json :156
serialize :222
igbinary_serialize :123
msgpack :102
json_encode :0.22339701652527
json_decode :0.53043985366821
serialize :0.31040406227112
unserialize :0.30859398841858
Igbinary Serialize: 0.25647687911987
Igbinary unSerialize: 0.19416117668152
msgpack_pack: 0.14058780670166
msgpack_unpack: 0.29048585891724
Copy after login

方便对比把之前PHP5.3的测试结果放到下面(之前并未测试igbinary)

json :156
serialize :222
json_encode :0.1087498664856
json_decode :0.12652111053467
serialize :0.041656017303467
unserialize :0.040987968444824
Copy after login

采用大数组测试结果
json :5350
serialize :8590
igbinary_serialize :2432
msgpack :3929
json_encode :0.92437314987183
json_decode :1.791629076004
serialize :1.3011419773102
unserialize :1.1485421657562
Igbinary Serialize: 0.90479803085327
Igbinary unSerialize: 0.69125699996948
msgpack_pack: 0.52022004127502
msgpack_unpack: 1.0104610919952
Copy after login

下面是之前的结果(之前并未测试igbinary)

json :5350
serialize :8590
json_encode :0.90479207038879
json_decode :1.753741979599
serialize :1.3566699028015
unserialize :1.3003630638123
Copy after login

小结:
数据方面:
      1:升级到PHP5.5后,json,serialize,igbinary三种方式序列化后,大小没有变化,说明这三种格式的对象结构没有没有变化,所以可以无缝升级,msgpack由于没有之前的数据做对比,暂时未知。
      2:占用空间方面,igbinary节省空间明显优势,比如在json一个数组5.4k大小的数据,serialize方式要8.6k,而使用igbinary方式,仅需2.4k,近乎为serialize方式的1/4,但在小数组方面msgpack方式更具优势,igbinary占用空间123,而msgpack方式仅为102。但是在大数组情况下,明显igbinary方式优势更明显。大数组igbinary胜出,小数组msgpack胜出。
性能方面:
      1:在小数据时,json和原生serialize的性能都比PHP5.3版本有所提升,而在处理大数据量时,性能又有所下降。
      2:在序列化方面,msgpack方式性能最好,其次是json_encode的,再次是igbinary,这两者相差无几,最差的为原生serialize,原生serialize性能消耗大概为json和igbinary方式的的1.4倍左右,而是msgpack方式的2倍。在大数组方面,序列化方便,基本上和小数组一致,只是igbinary性能教较json_encode方式有所提升。本轮msgpack胜出。
      3:在反序列方面igbinary的比序列化过程更快,当然也是最快的,但是这种快也是有成本代价的,参见最后的注意事项,最慢的为json_decode方式,猜测原因可能在于PHP作为服务器端应用,最多的场景是encode,而decode的最常见的为js处理方式,性能不是很理想。而msgpack反序列化性能基本上是它序列化的2倍。本轮igbinary胜出。
      4:整体性能对比,整体性能是序列化和反序列化之和,简单对比会发现,json是最差的,次之是原生serialize,再次为igbinary的方式,最优的为msgpack,不过igbinary和msgpack相差真的非常小,而在占用空间方面,小数据时msgpack胜出,大数据时igbinary胜出,算是各有千秋。所以,如果追求极致的性能,可以考虑使用msgpack,如果对是使用空间要求苛刻,那就选择igbinary方式,估计这也是PHPRedis选择igbinary作为内置序列化方式的原因之一,另外还有一个原因,考虑到Redis应用场景多是一写多读,要保证反序列化性能足够高,非igbinary莫属。

使用igbinary并非没有代价,在测试中我们发现,调用igbinary_unserialize时,传递非法数据,会导致整个php进程死掉,日志

child 19131 exited on signal 11 (SIGSEGV) after  1.844938 seconds from start 1.844938 seconds from start
Copy after login
估计是因为igbinary为了提升性能,在unserialize时,没有做相关格式验证,导致整个进程异常退出。在使用Redis时,我们先期使用SERIALIZE_PHP方式序列化,为了提升性能,减少对Redis空间的浪费采用igbinary_serialize方式,再切换的时候不小心踩到这个坑,导致服务器响应出错,直接502,幸亏在daily环境上。


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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

Performance comparison of C++ with other languages Performance comparison of C++ with other languages Jun 01, 2024 pm 10:04 PM

When developing high-performance applications, C++ outperforms other languages, especially in micro-benchmarks. In macro benchmarks, the convenience and optimization mechanisms of other languages ​​such as Java and C# may perform better. In practical cases, C++ performs well in image processing, numerical calculations and game development, and its direct control of memory management and hardware access brings obvious performance advantages.

How good is the performance of random number generators in Golang? How good is the performance of random number generators in Golang? Jun 01, 2024 pm 09:15 PM

The best way to generate random numbers in Go depends on the level of security required by your application. Low security: Use the math/rand package to generate pseudo-random numbers, suitable for most applications. High security: Use the crypto/rand package to generate cryptographically secure random bytes, suitable for applications that require stronger randomness.

See all articles