Home > Backend Development > PHP Tutorial > php5-memcached is slightly faster than php5-memcache

php5-memcached is slightly faster than php5-memcache

WBOY
Release: 2016-07-29 09:05:54
Original
1031 people have browsed it

??

php5-memcached is slightly faster than php5-memcache

php5-memcached and php5-memcache are two PHP components that operate memcached. They were developed by different people.

php official website lists their respective usage methods:

  • http://www.php.net/manual/en/book.memcache.php (Installation use: sudo apt-get install php5-memcache)
  • http://www.php.net/manual/en/book.memcached.php (Installation use: sudo apt-get install php5-memcached)
  • 1. First, First install apache:

    sudo apt-get update
    sudo apt-get install apache2

    2. Then install php5:

    sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

    3. Then install memcached:

    sudo apt-get install memcached

    4. Then install php5-memcached and php5-memcache:

    sudo apt-get install php5-memcache

    sudo apt-get install php5-memcached

    5. Finally restart apache2:

    sudo service apache2 restart

    6. Edit test.php as follows:

    <span><?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";
    ?></span>
    Copy after login

    7. Run http://machinename/test.php or php /var/www/html/test.php

    root@machinename # php /var/www/html/test.php
    memcache vs memcached : 10000 keys

    ~

    root@machinename

    # php /var/www/html/test2.phpmemcache vs memcached: 10000 keys
    Reference documents:
    1. https://www.digitalocean.com/community/tutorials/ how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

    2. https://www.digitalocean.com/community/tutorials/how-to-install-and-use-memcache -on-ubuntu-14-04

    3. https://www.leaseweb.com/labs/2013/03/memcache-vs-memcached-php-benchmark/

    The above has introduced that php5-memcached is slightly faster than php5-memcache, including some aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template