Performance Comparison between json and serialize in php

Source: Internet
Author: User
Four serialization methods, json_encode, serialize, igbinary, and msgpack, have been tested before. PHP5.5 has not been tested yet. this test is based on PHP5.5 and has test cases, h...

Four serialization methods, json_encode, serialize, igbinary, and msgpack, have been tested before. PHP5.5 has not been tested yet. this test is based on PHP5.5 and has test cases,
Http://blog.csdn.net/hguisu/article/details/7651730
The test cases are the same, but from this test home igbinary serialize test, as a comparison, you can refer
Http://www.ooso.net/archives/538

Running environment
PHP5.5 memory 16G 8 Core 2.0 GMHz

Performance & space size list

Use a small array of test results (note that for good data, when testing a small array, the number of cycles is 10000, and the large array is 1000)

  1. Json: 156

  2. Serialization: 222

  3. Igbinary_serialize: 123

  4. Msgpack: 102

  5. Json_encode: 0.22339701652527

  6. Json_decode: 0.53043985366821

  7. Serialization: 0.31040406227112

  8. Unserialized: 0.30859398841858

  9. Igbinary serialized: 0.25647687911987

  10. Igbinary unserialized: 0.19416117668152

  11. Msgpack_pack: 0.14058780670166

  12. Msgpack_unpack: 0.29048585891724


To facilitate the comparison, put the test results of PHP5.3 below (igbinary was not tested before)

  1. Json: 156

  2. Serialization: 222

  3. Json_encode: 0.1087498664856

  4. Json_decode: 0.12652111053467

  5. Serialization: 0.041656017303467

  6. Unserialized: 0.040987968444824


Test results using a large array

  1. Json: 5350

  2. Serialization: 8590

  3. Igbinary_serialize: 2432

  4. Msgpack: 3929

  5. Json_encode: 0.92437314987183

  6. Json_decode: 1.791629076004

  7. Serialization: 1.3011419773102

  8. Unserialized: 1.1485421657562

  9. Igbinary serialized: 0.90479803085327

  10. Igbinary unserialized: 0.69125699996948

  11. Msgpack_pack: 0.52022004127502

  12. Msgpack_unpack: 1.0104610919952


The following are the previous results (igbinary was not tested before)

  1. Json: 5350

  2. Serialization: 8590

  3. Json_encode: 0.90479207038879

  4. Json_decode: 1.753741979599

  5. Serialization: 1.3566699028015

  6. Unserialized: 1.3003630638123

Summary:
Data:
1: After being upgraded to PHP5.5, the size of the json, serialize, and igbinary formats does not change after being serialized. This means that the object structure of these three formats has not changed, so you can seamlessly upgrade them, msgpack is unknown because there is no comparison between the previous data.
2: In terms of space occupation, igbinary has obvious advantages in space saving. for example, for data of 8.6 k size in an array of json, the serialize method requires 2.4 k, while the igbinary method requires only k, almost 1/4 of the serialize method, but msgpack is more advantageous in small arrays. igbinary occupies 123 of the space, while msgpack is only 102. However, in the case of large arrays, the advantage of the igbinary method is more obvious. The big array igbinary wins, while the small array msgpack wins.
Performance:
1: for small data volumes, the performance of json and native serialize is improved compared with that of PHP5.3, while the performance decreases when processing large data volumes.
2: In terms of serialization, msgpack has the best performance, followed by json_encode, and igbinary, which is almost the same as native serialize, native serialize consumes about 1.4 times the performance of the json and igbinary methods, but twice the performance of the msgpack method. In terms of large arrays, serialization is convenient, basically the same as small arrays, but the igbinary performance is improved compared with the json_encode method. This round of msgpack won.
3: In terms of deserialization, igbinary is faster than the serialization process. of course, it is also the fastest, but this is also costly. For more information, see the final notes. the slowest method is json_decode, the reason for speculation may be that PHP is used as a server-side application and the most common scenario is encode. The most common decode method is js processing, and the performance is not ideal. The performance of msgpack deserialization is basically twice that of msgpack. This igbinary round won.
4: Overall performance comparison: the overall performance is the sum of serialization and deserialization. simple comparison shows that json is the worst, followed by native serialize, which is the igbinary method again, the optimal solution is msgpack, but the difference between igbinary and msgpack is really small. in terms of space occupation, msgpack wins when using small data and igbinary wins when using big data. Therefore, if you want to achieve the ultimate performance, you can consider using msgpack. if you are demanding on the use of space, you can choose the igbinary method, it is estimated that this is one of the reasons why PHPRedis chooses igbinary as the built-in serialization method. Another reason is that Redis is mostly one-Write-multiple-read, and the deserialization performance should be high enough to ensure that, non-igbinary does not belong.

The use of igbinary_unserialize is not costly. during the test, we found that when the igbinary_unserialize is called, passing illegal data will cause the entire php process to die.

  1. Child 19131 exited on signal 11 (SIGSEGV) after 1.844938 seconds from start 1.844938 seconds from start

It is estimated that in order to improve the performance of igbinary, no format verification is performed during unserialize, causing the entire process to exit unexpectedly. When using Redis, we first use the SERIALIZE_PHP method for serialization. to improve performance and reduce the waste of Redis space, we use the igbinary_serialize method. when switching, we accidentally step on this pitfall, this causes an error in the server response, which is directly 502, thanks to the daily environment.


Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.