Memcache vs Memcached PHP benchmark

We are talking about the following two PHP memcache clients.

Should you use memcache or memcached? There are some people writing about this:

Memcached 16% faster than Memcache?

Nobody seemed to benchmark the two clients to see which one is faster and that is why I did just that. I tested both clients on the same machine with default settings in Ubuntu 12.04 for both the clients and the server. This is the benchmark script I used:

<?php
// Initialize values: 10000 keys of 20 bytes with 40 bytes of data
$c = 10000;
$values = array();
for ($i=0;$i<$c;$i++) $values[sprintf('%020s',$i)]=sha1($i);
echo "memcache vs memcached: $c keys\n";
// Memcached
$m = new Memcached();
$m->addServer('localhost', 11211);
$start = microtime(true);
foreach ($values as $k => $v) $m->set($k, $v, 3600);
$time = microtime(true)-$start;
echo "memcached set: $time\n";
$start = microtime(true);
foreach ($values as $k => $v) $m->get($k);
$time = microtime(true)-$start;
echo "memcached get: $time\n";
// Memcache
$m = new Memcache();
$m->addServer('localhost', 11211);
$start = microtime(true);
foreach ($values as $k => $v) $m->set($k, $v, 0, 3600);
$time = microtime(true)-$start;
echo "memcache set: $time\n";
$start = microtime(true);
foreach ($values as $k => $v) $m->get($k);
$time = microtime(true)-$start;
echo "memcache get: $time\n";

And this is the output:


maurits@maurits-Aspire-X3960:~$ php memcache.php
memcache vs memcached: 10000 keys
memcached set: 0.91661500930786
memcached get: 0.86234307289124
memcache set: 1.0546097755432
memcache get: 1.0519700050354

We clearly see that memcached is faster than memcache, but to find out how much faster we have to use xdebug profiling (install with “sudo apt-get install php5-xdebug”). We enabled xdebug profiling by setting “xdebug.profiler_enable = 1” in “/etc/php5/conf.d/xdebug.ini”. After running the php script a “cachegrind.out” file is created in the “/tmp” directory. KCachegrind (install with “sudo apt-get install kcachegrind”) can analyze that file and make pretty graphs:

memcache_vs_memcached_3
picture 1: KCachegrind call graph

memcache_vs_memcached_2
picture 2: KCachegrind callee map

memcache_vs_memcached
picture 3: KCachegrind flat profile

Conclusion

The difference is really small, so there are probably better reasons to choose the memcached client over the memcache client 😉

Share

8 thoughts on “Memcache vs Memcached PHP benchmark”

  1. At HOSD we use the memcached extension for the shared webhosting platform because it’s a lot more stable than memcache-3.0.x.

    Ubuntu decided for some reason to include memcache-3.0.x in Ubuntu 12.04 LTS, although it is officially still marked beta in PECL and segfaults like crazy.

  2. I tested it with you code by 10X keys (100000 keys) on Centos 6.3 with 2X E5606 and 16 GB of 1333MHz RAM,
    This is my result :
    memcache vs memcached: 100000 keys
    memcached set: 5.2108051776886
    memcached get: 4.4858021736145
    memcache set: 7.3300750255585
    memcache get: 6.6995921134949
    memcached is faster than memcache.

  3. @Hans: That sounds indeed as a very good reason to choose memcached. Thank you for your comment, I did not know about that. It will definitely help people choose between the two!

  4. @Roohollah: Thank you for confirming my findings and taking the time to post a comment.

  5. My findings actually contradict yours; which surpises me. I also chose memcached for stability reasons; but it seems performance suffers. I have also increased the amount of keys x 10:

    memcache vs memcached: 100.000 keys
    memcached set: 2.1418437957764
    memcached get: 1.6363079547882
    memcache set: 2.0986111164093
    memcache get: 1.74343085289

    Subsequent runs produce the same results. This is on a i7-2600K. I am using these versions:

    libmemcached 1.0.16-1
    memcached 1.4.15-1
    php-memcached 2.1.0-1

  6. Jumping on this old post. Memcached client huuge downside at the moment is that it cannot use persistent TCP connections from what I have seen.

    So multiple memcached clients in different geographical regions, will use new TCP connection for each call to memcached server => RTT overhead which could be higher than the overhead memcache client has over ‘d client.

    If stability is solved in future versions of pecl-memcache-3.x or 4.x (who knows?), and scalability over geographical regions is a requirement, than php-memcached/pecl-memcached is next to useless.

    Andrei.

  7. Test on Fedora 20:

    memcache vs memcached: 10000 keys
    memcached set: 0.24293112754822
    memcached get: 0.26972103118896
    memcache set: 0.066087007522583
    memcache get: 0.063021183013916

    Test on Centos 6.6:
    memcache vs memcached: 10000 keys
    memcached set: 0.70846891403198
    memcached get: 0.6068639755249
    memcache set: 0.76999282836914
    memcache get: 0.73713994026184

    The Fedora results are very strange ,memcache class is too fast

Leave a Reply

Your email address will not be published. Required fields are marked *