Skip to content

Instantly share code, notes, and snippets.

@roadrunner
Created January 8, 2012 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roadrunner/1577462 to your computer and use it in GitHub Desktop.
Save roadrunner/1577462 to your computer and use it in GitHub Desktop.
Speed comparison for APC & Array look-up
<?php
if(!extension_loaded('apc')){
die("APC extension is not enabled.");
}
function generate_class_map($size){
apc_clear_cache('user');
$map = array();
$class_prefix = '\Acme\Foo\Bar\Baz\Class';
$file_prefix = '/path/to/classes/acme/foo/bar/baz';
for($i = 0; $i < $size; $i++){
$map[$class_prefix.'_'.$i] = $file_prefix.'_'.$i.'.php';
apc_store($class_prefix.'_'.$i, $map[$class_prefix.'_'.$i]);
}
return $map;
}
function test_array(&$map, $class_name){
if(isset($map[$class_name])){
$file = $map[$class_name];
// include the file
}
}
function test_apc($class_name){
$success = false;
$file = apc_fetch($class_name, $success);
if($success){
// include the file
}
}
$current = $start = 100;
$max = 100000;
if(php_sapi_name() != 'cli'){
header('Content-Type: text/plain');
}
do{
$map = generate_class_map($current);
$start_time = microtime(true);
for($i = 0; $i < $current; $i++){
test_array($map, '\Acme\Foo\Bar\Baz\Class_'.$i);
}
$array_time = microtime(true) - $start_time;
$start_time = microtime(true);
for($i = 0; $i < $current; $i++){
test_apc('\Acme\Foo\Bar\Baz\Class_'.$i);
}
$apc_time = microtime(true) - $start_time;
echo sprintf("Result for %d items\n", $current);
echo sprintf("%-12s: %01.8f\n", "APC time", $apc_time);
echo sprintf("%-12s: %01.8f\n", "Array time", $array_time);
echo sprintf("%-12s: %01.8f [%% %01.2f]\n", "APC - Array", ($apc_time - $array_time), (100* ($apc_time / $array_time)) - 100);
echo "-------------------------\n";
$current *= 10;
}while($current <= $max);
Result for 100 items
APC time : 0.00013709
Array time : 0.00010800
APC - Array : 0.00002909 [% 26.93]
-------------------------
Result for 1000 items
APC time : 0.00132179
Array time : 0.00094199
APC - Array : 0.00037980 [% 40.32]
-------------------------
Result for 10000 items
APC time : 0.01758194
Array time : 0.01026487
APC - Array : 0.00731707 [% 71.28]
-------------------------
Result for 100000 items
APC time : 0.38990903
Array time : 0.11457086
APC - Array : 0.27533817 [% 240.32]
-------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment