Last active
August 29, 2015 14:27
-
-
Save atdt/02b85851cae770d7fef3 to your computer and use it in GitHub Desktop.
Simple benchmark demonstrating the efficiency of APC compared to memcached. Even if the memcached instance is running on localhost, it is a separate process, and PHP has to talk with it via a socket (usually TCP, sometime UNIX). This requires executing many system calls, like select(), accept(), read(), and write(), each of which crosses the bou…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$loops = 1000; | |
$data = array( 'a' => 1, 'b' => 2, 'c' => 3 ); | |
// Memcached | |
$cache = new Memcached(); | |
$cache->addServer( "127.0.0.1", 11211 ); | |
$start = microtime( true ); | |
for ( $i = 0; $i < $loops; $i++ ) { | |
$cache->set( 'speed-test', $data ); | |
$cache->get( 'speed-test' ); | |
} | |
$avg = 1e6 * ( ( microtime( true ) - $start ) / $loops ); | |
printf( "Memcached::set() / Memcached::get(): %.2fμs\n", $avg ); | |
// APC | |
$start = microtime( true ); | |
for ( $i = 0; $i < $loops; $i++ ) { | |
apc_store( 'speed-test', $data ); | |
apc_fetch( 'speed-test' ); | |
} | |
$avg = 1e6 * ( ( microtime( true ) - $start ) / $loops ); | |
printf( "apc_store() / apc_fetch(): %.2fμs\n", $avg ); | |
// Sample output: | |
// | |
// Memcached::set() / Memcached::get(): 80.23μs | |
// apc_store() / apc_fetch(): 2.33μs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few things to be aware of when moving caches from memcached to APC:
Ignoring any or all of these differences may make things much worse rather than much better, but if you are aware of these at the application level APC can certainly be much much faster.