Explanation of the performance of PHP Implode/explode, serialize, JSON, Msgpack

Source: Internet
Author: User
PHP Implode/explode, serialize, JSON, msgpack performance comparison

First use implode, serialize, Json_encode, Msgpack_pack to create four text files for testing.

Create the following code:

<?php$arr = Array (    ' content1 ' = ' 1,234,567,890 ',    ' content2 ' = ' 1,234,567,890 ',    ' content3 ' = ' = ') 1,234,567,890 '); Echo file_put_contents (' Implode.txt ', implode (', ', $arr), true). ' <br> '; Echo file_put_contents (' Serialize.txt ', serialize ($arr), true). ' <br> '; Echo file_put_contents (' Json.txt ', Json_encode ($arr), true). ' <br> '; Echo file_put_contents (' Msgpack.txt ', Msgpack_pack ($arr), true);? >


Build after creation

Implode.txt 92 bytes
Serialize.txt 165 bytes
Json.txt 223 bytes
Msgpack.txt 121 bytes

The resulting string size is sorted as follows implode < Msgpack_pack < Serialize < Json_encode

If the array is simple, then json_encode may be smaller than serialize

For example:

$arr = Array (' One ', ' two ', ' three ', ' four ', ' five ', ' VI ', ' VII ', ' VIII ', ' IX ', ' ten ');

Serialize is 147 bytes

Json_encode is 91 bytes

Compare implode, serialize, Json_encode, msgpack_pack performance

<?php$arr = Array (    ' content1 ' = ' 1,234,567,890 ',    ' content2 ' = ' 1,234,567,890 ',    ' content3 ' = ' = ') 1,234,567,890 '); $start = Microtime (true); $i = 1000000;while ($i >0) {    //test run time and memory usage respectively    $tmp = Implode (', ', $arr );    $tmp = serialize ($arr);    $tmp = Json_encode ($arr);    $tmp = Msgpack_pack ($arr);    $i--;} $end = Microtime (true); Echo ' Run Time: '. ($end-$start). ' S<br> '; Echo ' memory usage: '. (Memory_get_usage ()/1024). ' KB ';? >
Implode       1.3225722312927s    628.50KBserialize     2.0553789138794s    628.32kbjson_encode   2.5058920383453s    628.34kbmsgpack_pack  1.6431028842926s    628.24KB

Result: Memory usage is similar, run time Implode < Msgpack_pack < Serialize < Json_encode

Compare explode, Unserialize, Json_decode, msgpack_unpack performance

<?php$data = file_get_contents (' implode.txt ');//$data = file_get_contents (' serialize.txt ');//$data = file_get_ Contents (' Json.txt ');//$data = file_get_contents (' msgpack.txt '); $start = Microtime (true); $i = 1000000;while ($i >0) {    $tmp = explode (', ', $data);    $tmp = Unserialize ($data);    $tmp = Json_decode ($data, true);    $tmp = Msgpack_unpack ($data);    $i--;} $end = Microtime (true); Echo ' Run Time: '. ($end-$start). ' S<br> '; Echo ' memory usage: '. (Memory_get_usage ()/1024). ' KB ';? >
Explode         1.7446749210358s    628.74KBunserialize     2.1386790275574s    628.67kbjson_decode     5.2423169612885s    628.84kbmsgpack_unpack  2.2290098667145s    628.63KB

Results: Memory usage is similar, run time explode < serialize < Msgpack_unpack < Json_decode

It is concluded that because Implode/explode is not suitable for complex structures, it is commonly used for serialize,json,msgpack of three kinds.

and three kinds of comparison, running speed, memory consumption, the space occupies the best msgpack, followed by Serialize, and finally JSON.

It is recommended to use Msgpack serialization to process the data if conditions are available.

About Msgpack You can view the article I wrote earlier: "Messagepack serialization Format"

This article compares the performance between PHP Implode/explode, serialize, JSON, Msgpack, and more about PHP Chinese web.

Related recommendations:

PHP str_replace to replace the specified number of times method explanation

About Header,headers_sent,headers_list,header_remove Usage Instructions

Query MySQL return field by PDO the integer type becomes a string-based workaround

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.