Comparison between PHP serialization and JSON format

Source: Internet
Author: User

When using mc, we will consider the data storage format. The PHP function allows us to directly set an array to MC. After obtaining it, we can also use it as an array, which is really simple, however, after comparison with the JSON format, we found that the simple cost is a waste of more space. Below is a small test program:

<? PHP
$ Host = "10.10.10.10 ";
$ Port = "11211 ";

$ Uid = "103630 ";
$ P1 = array (
"ET" => "103630", // expire time
"PT" => "103630 ",//
"PV" = & gt; "103630 ",
"Ut" = & gt; "103630 ",
"St" => "103630 ",
"PP" => "103630 ",
"S" => "103630 ",
"OT" => "103630"

);
$ Data [] = $ P1;
$ MC = new memcache ();
$ Mc-> connect ($ host, $ port );
$ Mc-> set (103631, $ data );
$ Mc-> set (103632, json_encode ($ data ));
?>

Telnet 10.10.10.10 11211
Get 103631
Value 103631 1 191
A: 1: {I: 0; A: 8: {s: 2: "ET"; s: 6: "103630"; s: 2: "PT"; s: 6: "103630"; s: 2: "PV"; s: 6: "103630"; s: 2: "ut"; s: 6: "103630"; s: 2: "St"; s: 6: "103630"; s: 2: "PP"; s: 6: "103630"; s: 1: "S"; s: 6: "103630"; s: 2: "OT"; s: 6: "103630 ";}}
End
Get 103632
Value 103632 0 114
[{"ET": "103630", "PT": "103630", "PV": "103630", "ut": "103630", "St ": "103630", "PP": "103630", "S": "103630", "OT": "103630"}]
End

 

It can be seen that JSON still saves memory space.

However, PHP can serialize all types except the resource type. JSON can only be serialized in four ways, and the expression is very concise.

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.