note 84298 added to function.var-export
| From: | sypek dot pawel at gmail dot com | Date: | Tue, 08 Jul 2008 13:15:25 +0000 |
| Subject: | note 84298 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141266@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 82.160.94.10
Probable Submitter: 194.110.240.132
----
Manual Page -- http://www.php.net/manual/en/function.var-export.php
Edit -- https://master.php.net/note/edit/84298
Del: integrated -- https://master.php.net/note/delete/84298/integrated
Del: useless -- https://master.php.net/note/delete/84298/useless
Del: bad code -- https://master.php.net/note/delete/84298/bad+code
Del: spam -- https://master.php.net/note/delete/84298/spam
Del: non-english -- https://master.php.net/note/delete/84298/non-english
Del: in docs -- https://master.php.net/note/delete/84298/in+docs
Del: other reasons-- https://master.php.net/note/delete/84298
Reject -- https://master.php.net/note/reject/84298
Search -- https://master.php.net/manage/user-notes.php