Use var_export and serialize in PHP to test the array cache performance.

Source: Internet
Author: User
In general, if we cache the array into a file, we need to convert it into a string first, and then store it in a text file. Generally, there are two mechanisms to convert the array into a string,

In general, if we cache the array into a file, we need to convert it into a string first, and then store it in a text file. Generally, there are two mechanisms to convert the array into a string,

The first type is

$ Str = var_export ($ arr, true );
The second type is
$ Str = serialize ($ arr );
The converted strings are different from each other. The first is the prototype of the array, and the second is the serialized form. The first method is to add Tag, in the form of an available array prototype, for the call, you do not need to convert, you can directly return this array, but the second type, you need to use the unserialize function to deserialize it. For the first statement, one more step is required. Let's talk about it with data:
Set_time_limit (50 );
$ A = array (1, 2, 3 );
$ B = array ('a' => 1, 'B' => 2, 'C' => 3 );
$ C = array ('a' => array (1, 2, 3), 'B' => array (4, 5, 6 ));

$ Time1 = microtime (true );

$ Times = 1000000; #10 w

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ A = var_export ($ a, true );
}

$ Time2 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ B = var_export ($ B, true );
}

$ Time3 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ C = var_export ($ c, true );
}

$ Time4 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ X = serialize ($ );
}

$ Time5 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ Y = serialize ($ B );
}

$ Time6 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ Z = serialize ($ c );
}

$ Time7 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ O = unserialize ($ X );
}

$ Time8 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ P = unserialize ($ Y );
}

$ Time9 = microtime (true );

For ($ I = 1; $ I <= $ times; $ I ++ ){
$ Q = unserialize ($ Z );
}
$ Time10 = microtime (true );

$ Var_export_time ['A'] = $ time2-$ time1;
$ Var_export_time ['B'] = $ time3-$ time2;
$ Var_export_time ['c'] = $ time4-$ time3;

$ Serialize_time ['A'] = $ time5-$ time4;
$ Serialize_time ['B'] = $ time6-$ time5;
$ Serialize_time ['c'] = $ time7-$ time6;

$ Unserialize_time ['A'] = $ time8-$ time7;
$ Unserialize_time ['B'] = $ time9-$ time8;
$ Unserialize_time ['c'] = $ time10-$ time9;
Print_r ($ var_export_time );
Print_r ($ serialize_time );
Print_r ($ unserialize_time );
?>
Output:

Array
(
[A] = & gt; 3.3401498794556
[B] = & gt; 5.1394801139832
[C] = & gt; 8.8483898639679
)
Array
(
[A] = & gt; 1.6063709259033
[B] = & gt; 1.7033960819244
[C] = & gt; 3.4534389972687
)
Array
(
[A] = & gt; 1.6037359237671
[B] = & gt; 1.817803144455
[C] = & gt; 3.7992968559265
)

Data description:
The performance of the var_export function is twice worse than that of the serialize function, and the unserialize time also needs to be about the same as that of the serialize function. the added time of serialize and unserialize is about the same as the var_export time.
Therefore, when the application is only used to read data, it is best to save it as an array prototype. if you only want to write data to the cache, using serialize is a good choice, in addition, serialize can also process the Object type. Therefore, it can be widely used.

However, if we talk about the size of the cached file after generation, we still use var_export to remove line breaks and white spaces in the array, which is about 10% smaller than serialize. I will not put this test on, if you are interested, try it. The reason is that some redundant strings are added to the serialize function.

This article

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.