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 is $ strvar_export ($ arr, true); the second is $ strserialize ($ arr );? Set_time_limit (50); $ aarray (1, 2, 3); $ barray (
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 is $ str = var_export ($ arr, true); the second is $ str = serialize ($ arr );? Set_time_limit (50); $ a = array (1, 2, 3); $ B = array ('
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 );
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.