PHP Messagepack Introduction _php Tutorial

Source: Internet
Author: User
Tags object serialization php website
1, today in Hacknews saw a lot of people on the Messagepack controversy. First understand what messagepack:messagepack is a binary-based efficient object serialization library. IT enables to exchange structured objects between many languages like JSON. But unlike JSON, it's very fast and small.

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

3, the controversial place is Messagepack's benchmark said he is much faster than protocolbuffer,json many times. But some people do not believe, do a javasript under the test (JSON and Messagepack). It is found that the compressed data of Messagepack is about 10% less than JSON, while compression and decompression time is much more expensive than JSON parser.

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

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

Copy the Code code as follows:
myjsonstring = Json.stringify (MyObject);
MyObject = Json.parse (myjsonstring);
var Mybytearray = Msgpack.pack (MyObject);
MyObject = Msgpack.unpack (Mybytearray);

Messagepack The author also thinks Messagepack May is the best choice for client-side serialization as described by the blog author. Reference 2 A little tragedy.

5,bson is a binary form of JSON, but it is not syntactically compatible with JSON. But Messagepack promises to be semantically consistent.

6, the requirements of the scene are different, resulting in the application of the technology vary.

PHP Trial Messagepack

It ' s like JSON. But fast and small.

This sentence attracted me, to see the next.

Official website: http://msgpack.org

The official installation method is Msgpack, there is no PHP directory under the directory ... Only see directories such as Csharp,erlang,go,java,ruby.

Copy CodeThe code is as follows:
git clone https://github.com/msgpack/msgpack.git
CD msgpack/php
Phpize
./configure && make && make install


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

Copy CodeThe code is 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 ');

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

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

In addition, the GBK program is convenient, the Chinese can also msgpack_pack, with the JSON word also to be converted into utf-8 after batch to Json_encode.

Reference:

1,messagepack official website

2,msgpack vs. Json:cut your Client-server exchange traffic by 50% with 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

http://www.bkjia.com/PHPjc/313487.html www.bkjia.com true http://www.bkjia.com/PHPjc/313487.html techarticle 1, today in Hacknews saw a lot of people on the Messagepack controversy. First understand what messagepack:messagepack is a binary-based efficient object serialization library. It Enables ...

  • 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.