Performance testing of array caching using Var_export and serialize in PHP ____php

Source: Internet
Author: User

In general, we cache the array into a file, the first to be converted into a string form, and then into the text file, there are generally two mechanisms to convert the array into a string,

The first is $str = Var_export ($arr, true);
The second type is
$str = serialize ($arr);

The strings after the conversion of these two mechanisms are not the same, the first is the prototype pattern of the array, and the second is the serialized form. The first deposit in the file as long as the addition of <?php?> tag, the form of an available array prototype, for the call, without conversion, directly return to the array can be, but the second, you need to use the unserialize function of the reverse serialization. For the first one, there is one more step. Come down and we'll talk with the 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));

$time 1 = microtime (true); $times = 1000000;

#10w for ($i =1 $i <= $times; $i + +) {$A = Var_export ($a, true);

$time 2 = Microtime (true);

For ($i =1 $i <= $times $i + +) {$B = Var_export ($b, true);

$time 3 = Microtime (true);

For ($i =1 $i <= $times $i + +) {$C = Var_export ($c, true);

$time 4 = Microtime (true);

for ($i =1; $i <= $times; $i + +) {$X = serialize ($a);}

$time 5 = Microtime (true);

for ($i =1; $i <= $times; $i + +) {$Y = serialize ($b);}

$time 6 = Microtime (true);

for ($i =1; $i <= $times; $i + +) {$Z = serialize ($c);}

$time 7 = Microtime (true);

for ($i =1; $i <= $times; $i + +) {$O = Unserialize ($X);}

$time 8 = Microtime (true);

for ($i =1; $i <= $times; $i + +) {$P = Unserialize ($Y);}

$time 9 = Microtime (true);

For ($i =1 $i <= $times $i + +) {$Q = Unserialize ($Z);} $time = Microtime (true); $var _export_time[' a '] = $time 2-$timE1;
$var _export_time[' b '] = $time 3-$time 2;

$var _export_time[' c '] = $time 4-$time 3;
$serialize _time[' a '] = $time 5-$time 4;
$serialize _time[' b '] = $time 6-$time 5;

$serialize _time[' c '] = $time 7-$time 6;
$unserialize _time[' a '] = $time 8-$time 7;
$unserialize _time[' b '] = $time 9-$time 8;
$unserialize _time[' c '] = $time 10-$time 9;
Print_r ($var _export_time);
Print_r ($serialize _time); Print_r ($unserialize _time);?>

Output

Array
(
[A] => 3.3401498794556
[b] => 5.1394801139832
[C] => 8.8483898639679
)
Array
(
[A] => 1.6063709259033
[b] => 1.7033960819244
[C] => 3.4534389972687
)
Array
(
[A] => 1.6037359237671
[b] => 1.817803144455
[C] => 3.7992968559265
)

The above data indicate:
The performance of the Var_export function is one-fold worse than the performance of the Serialize function, and unserialize time needs to be about the same time as serialize, serialize plus unserialize time, and Var_export time.
So in the application, if only for reading data, it is best to save the number of prototypes, if only consider write cache, with Serialize is a good choice, there is serialize can also handle object type. So the application can be a wide range.

But if the cache file size after the generation, it is still using Var_export to remove the array of line and blank, compared with serialize to small about 10%, this test I will not put up, interested in their own can try. The reason is because some redundant strings are added to the Serialize function.

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.