Skip to content

Instantly share code, notes, and snippets.

@atdt
Last active August 29, 2015 14:27
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 atdt/02b85851cae770d7fef3 to your computer and use it in GitHub Desktop.
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…
<?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
@bd808
Copy link

bd808 commented Aug 10, 2015

A few things to be aware of when moving caches from memcached to APC:

  • APC has no concept of LRU eviction so when your cache is full and you want to add new things you can't until TTLs expire or something does a manual cleanup.
  • HHVM's APC implementation uses an unbounded pool so if you keep sticking data into it you can risk running the entire process out of memory.
  • APC is process local cache so it can't be used share data with another host

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment