Skip to content

Instantly share code, notes, and snippets.

@robo47
Created February 1, 2012 21:00
Show Gist options
  • Save robo47/1719293 to your computer and use it in GitHub Desktop.
Save robo47/1719293 to your computer and use it in GitHub Desktop.
comparing speed of fetching single elements from apc vs. accessing an array
<pre>
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
if (!isset($_GET['num'])) {
die('you forgot a parameter');
}
echo 'Doing ' . $_GET['num'] . ' entries' . PHP_EOL . PHP_EOL;
$map = array();
$data = '<?php return array( ';
for ($i = 1; $i < $_GET['num']; $i++)
{
$map[md5($i)] = sha1($i);
apc_add(md5($i), sha1($i));
$data .= " '". md5($i) . "' => '" . sha1($i) . "',";
}
$data .= ');';
file_put_contents('map.' . $_GET['num'] . '.php', $data);
unset($data);
$info = apc_sma_info();
echo 'APC used: ' . number_format((($info['seg_size'] - $info['avail_mem'] )/(1024*1024)),1);
echo '/' . number_format(($info['seg_size'] /(1024*1024)),1) . PHP_EOL . PHP_EOL;
$time = microtime(true);
foreach ($map as $key => $value)
{
$fetchedValue = apc_fetch($key);
}
$apctime = (microtime(true) - $time);
echo 'time apc: '.number_format($apctime, 10).PHP_EOL;
$time = microtime(true);
$map2 = include __DIR__ . '/map.' . $_GET['num'] . '.php';
foreach ($map as $key => $value)
{
$fetchedValue = $map2[$key];
}
$arraytime = (microtime(true) - $time);
echo 'time array: '.number_format($arraytime,10).PHP_EOL;
if ($arraytime < $apctime)
{
echo PHP_EOL.PHP_EOL.'array is faster';
}
else
{
echo PHP_EOL.PHP_EOL.'apc is faster';
}
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment