note 84298 deleted from function.var-export by bjori
| From: | bjori@php.net | Date: | Sat, 09 Aug 2008 17:22:05 +0000 |
| Subject: | note 84298 deleted from function.var-export by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143332@lists.php.net to get a copy of this message | ||
Note Submitter: sypek dot pawel at gmail dot com
----
To stangelanda at arrowquick dot com:
serialize()'d array has to be unserialize()'d when the data needs to be loaded, which adds
a significant (according to my own benchmark tests) overhead compared to using an include function.
I actually find 'include' as the fastest possible way to retrieve cached
(var_export()'ed) PHP arrays. Consider the code:
<?php
$ary = array();
function random_str($len = 0)
{
static $chars = 'ABCDEFHIJKLMNOPQRSTUVWXYZabcdefhgijklmnopqrstuvwxyz0123456789';
if($len === 0)
$len = rand(5, 20);
$ret = '';
for($i = 0;$i < $len;++$i)
{
$ret .= $chars[rand(0, 60)];
}
return $ret;
}
function random_val($array = true)
{
switch(rand(0, $array ? 4 : 3))
{
case 0: // random integer
return rand(0, 2000);
case 1: // random float
return floatval(rand(0, 2000) . '.' . rand(0, 99));
case 2: // random bool
return rand(1,2) === 2 ? true : false;
case 3: // random string
return random_str();
case 4: // random array
$ary = array();
for($i = 0;$i < 20;++$i)
{
$ary[] = random_val(false);
}
return $ary;
default:
return 0;
}
}
for($i = 0;$i < 1500;$i++)
{
if(rand(1,2) === 2)
$ary[] = random_val(true);
else
$ary[random_str()] = random_val(true);
}
/*
the code above is used to generate a random array with at most 1500 elements, which consists of
randomly generated integers, floats, booleans, strings and arrays. The array's keys might be
integers or strings.
*/
$t = microtime(true);
header("Content-type: text/plain");
echo 'Saving serialized data 1000 times: ';
flush();
for($i = 0;$i < 1000;++$i)
{
file_put_contents('t_serialize.php', '<?php return \'' .
serialize($ary) . '\'; ?>');
}
echo microtime(true) - $t . "\n";
$t = microtime(true);
echo 'Saving var_exported data 1000 times: ';
flush();
for($i = 0;$i < 1000;++$i)
{
file_put_contents('t_var_export.php', '<?php return ' . var_export($ary,
true) . '; ?>');
}
echo microtime(true) - $t . "\n";
$t = microtime(true);
echo 'Loading serialized data 1000 times: ';
flush();
for($i = 0;$i < 1000;++$i)
{
$ary = unserialize(include 't_serialize.php');
}
echo microtime(true) - $t . "\n";
$t = microtime(true);
echo 'Loading var_exported data 1000 times (include): ';
flush();
for($i = 0;$i < 1000;++$i)
{
$ary = include 't_var_export.php';
}
echo microtime(true) - $t . "\n";
/*
this needs to be done, because eval()'d code musn't include PHP tags (<?php and
?>)
*/
file_put_contents('t_var_export_eval.php', var_export($ary, true));
$t = microtime(true);
echo 'Loading var_exported data 1000 times (eval): ';
for($i = 0;$i < 1000;++$i)
{
$ary = eval('return ' . file_get_contents('t_var_export_eval.php') .
';');
}
echo microtime(true) - $t . "\n";
$t = microtime(true);
?>
My results are:
Saving serialized data 1000 times: 18.801375150681
Saving var_exported data 1000 times: 18.207436084747
Loading serialized data 1000 times: 52.228559017181
Loading var_exported data 1000 times (include): 18.555495977402
Loading var_exported data 1000 times (eval): 18.961781024933
Which ends in the conclusion that var_export()'ing is slightly faster than serialize()'ing
data, and include()'ing the exported data is almost 3 times faster than unserialize()'ing
the serialize()'d data.