PHP messagepack Introduction _php Tips

Source: Internet
Author: User
Tags object serialization php website serialization git clone

1, today in Hacknews to see a lot of people on the messagepack debate. First understand what is the messagepack:messagepack is a binary-based efficient object serialization library. IT enables to exchange structured objects between many like JSON. But Unlike JSON, it is very fast and small.

The main uses of 2,messagepack, the author explains that there are two major uses: First, space-efficient storage for Memcache entries (Pinterest), save space type Mamcache applications , and the other is for RPC transmissions, this using case is fairly close to my original intent. When you are designing an RPC system, one of the the ' the ' is ' to specify and implement a communication protocol. This process can get pretty hairy as your need to worry about a lot the low-level of like issues. By using Messagepack, one can skip designing and implementing a communication protocol entirely and accelerate development .

3, the disputed place is Messagepack's benchmark, who says he is many times faster than Protocolbuffer,json. But some people don't believe it, do a javasript test (JSON and Messagepack). Messagepack found that only compressed data is about 10% less than JSON, while compression and decompression times are much more time-consuming than JSON parsers.

4, "Msgpack vs. Json:cut your Client-server exchange traffic by 50% and one line of code" This article uses Messagepack to do server optimization, reducing server Data volume, more reasonable use of bandwidth. The authors stress that they would rather waste client 0.5ms-1ms, but the server uses Ruby's Messagepack parser to be 5 times times faster than JSON.

The difference to JSON are, that msgpack are binary-based -This gives the possibility to make the Exchang Ed data a) smaller and use less bytes, I guess we are know the advantages of that, however there are an even Bigge R Advantage: B) It is faster to parse and encode, has a parser parse-bytes takes about twice as Lo Ng as parsing bytes.

Copy Code code as follows:

myjsonstring = Json.stringify (MyObject);
MyObject = Json.parse (myjsonstring);
var Mybytearray = Msgpack.pack (MyObject);
MyObject = Msgpack.unpack (Mybytearray);

Messagepack author also believes that Messagepack May is the best choice for client-side serialization as described by the blog author. Quote 2 A little tragedy.

5,bson is a binary form of JSON, but is not compatible with JSON. But Messagepack guarantee semantic consistency.

6, the scene requirements are different, resulting in the application of the technology has a difference.

PHP Trial Messagepack

It ' s like JSON. But fast and small.

The sentence attracted me and went to see the next.

Official website: http://msgpack.org

The official installation method is Msgpack, there is no PHP directory in the directory ... See only directories such as Csharp,erlang,go,java,ruby.

Copy Code code as follows:

git clone https://github.com/msgpack/msgpack.git
CD msgpack/php
Phpize
./configure && make && make install


Or the expansion of the PHP website found: http://pecl.php.net/package/msgpack
Last updated: 2012-09-14, yesterday's version.
Attached installation process:

Copy Code code as follows:

wget http://pecl.php.net/get/msgpack-0.5.2.tgz
Tar zxf msgpack-0.5.2.tgz
CD msgpack-0.5.2
/usr/local/hx/php/bin/phpize
./configure--with-php-config=/usr/local/hx/php/bin/php-config
Make && make install


Then add the msgpack.so to the php.ini, restart PHP, and complete the installation.

To start the test:
$data = Array (0=> ' abcdefghijklmnopqrstuvwxyz ',1=> ' Xiamen ', ' abc ' => ' 1234567890 ');

The length of the msgpack_pack,json_encode,serialize is: 50,62,87
Then execute 10,000 times, time consuming: 9.95 milliseconds, 17.45 milliseconds, 8.85 milliseconds
Undo Execution 10,000 Times: 14.76 milliseconds, 23.93 milliseconds, 14.61 milliseconds

Msgpack performance of at least more than json50%, although and serialize in fact the speed is similar, but the serialize occupy more space.

In addition, the GBK program is convenient, Chinese can also msgpack_pack, with JSON words also want to convert batch into Utf-8 before Json_encode.

Reference:

1,messagepack official website

2,msgpack vs. Json:cut your Client-server exchange traffic by 50% and one line of code

HN Comment Address: http://news.ycombinator.com/item?id=4090831

3,my Thoughts on Messagepack

HN Comment Address: http://news.ycombinator.com/item?id=4092969

Comparison of Messagepack and JSON performance under 4 JS

HN Comment Address: http://news.ycombinator.com/item?id=4091051

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.