• 热门专题

php5memcached比php5memcache速度略快一点

作者:  发布日期:2016-01-21 19:56:18
Tag标签:速度  
  • php5-memcached比php5-memcache速度略快一点

     

    php5-memcached和php5-memcache是两个php操作memcached的组件,他们是不同人开发的。

    php官网都列出了他们各自的使用方法:

    http://www.php.net/manual/en/book.memcache.php (安装使用:sudo apt-get install php5-memcache)http://www.php.net/manual/en/book.memcached.php (安装使用:sudo apt-get install php5-memcached)

    1. 首先,先安装下apache:

    sudo apt-get update
    sudo apt-get install apache2

    2. 然后安装下php5:

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

    3. 接着安装memcached:

    sudo apt-get install memcached

    4. 接着安装php5-memcached和php5-memcache:

    sudo apt-get install php5-memcache

    sudo apt-get install php5-memcached

    5. 最后重启下apache2:

    sudo service apache2 restart

    6.编辑test.php内容如下:

    <?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
    ";
    
    // 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
    ";
    $start = microtime(true);
    foreach ($values as $k => $v) $m->get($k);
    $time = microtime(true)-$start;
    echo "memcached get: $time
    ";
    
    // 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
    ";
    $start = microtime(true);
    foreach ($values as $k => $v) $m->get($k);
    $time = microtime(true)-$start;
    echo "memcache get: $time
    ";
    ?>

    7. 运行http://machinename/test.php 或者 php /var/www/html/test.php

    root@machinename # php /var/www/html/test.php
    memcache vs memcached: 10000 keys
    memcached set: 0.7015380859375
    memcached get: 0.61220598220825
    memcache set: 0.78830289840698
    memcache get: 0.74954390525818

    ~
    root@machinename # php /var/www/html/test2.php
    memcache vs memcached: 10000 keys
    memcache set: 0.78771591186523
    memcache get: 0.75219798088074
    memcached set: 0.69968199729919
    memcached get: 0.60679888725281

    参考文档:

    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/

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规