Demo code for the same server using cache APC efficiency above memcached _php tutorial

Source: Internet
Author: User
Tags apc
Copy CodeThe code is as follows:
$memcachehost = ' localhost ';
$memcacheport = ' 11211 ';
function Microtime_float () {
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}
function Runtime ($t 1) {
Return Number_format ((Microtime_float ()-$t 1) *1000, 4). ' Ms ';
}
$starttime = Microtime_float ();
$cache _time = ' 30 ';
echo "init=====". Runtime ($starttime). '
';

$sql = "SELECT * FROM HX WHERE id = 10006";
$mem _sql_key = MD5 ($sql);

$t 1 = microtime_float ();
echo "apc_read=====";
$arrs = Apc_fetch ($mem _sql_key);
Echo Runtime ($t 1). '
';

$t 1 = microtime_float ();
Apc_store ($mem _sql_key. ' _test ', $arrs, $cache _time);
echo "apc_write=====";
Echo Runtime ($t 1). '
';

$t 1 = microtime_float ();
$mem = new Memcache;
$mem->connect ($memcachehost, $memcacheport);
echo "mem_connet=====". Runtime ($t 1). '
';

$t 1 = microtime_float ();
$arrs = $mem->get ($mem _sql_key);
echo "mem_read=====";
Echo Runtime ($t 1). '
';
$t 1 = microtime_float ();
$mem->set ($mem _sql_key. ' _test ', $arrs, 0, $cache _time);
echo "mem_write=====";
Echo Runtime ($t 1). '
';
?>

The results of this SQL are cached in both APC and memcached, and then the read-write speed is tested.
Results on native windows:
Init=====0.0341ms
Apc_read=====0.0439ms
Apc_write=====0.0920ms
Mem_connet=====11.0571ms
Mem_read=====0.2630ms
Mem_write=====0.2270ms

Results on Linux on the server:
Init=====0.0131ms
Apc_read=====0.0520ms
Apc_write=====0.0489ms
Mem_connet=====0.0501ms
Mem_read=====0.1030ms
Mem_write=====0.0801ms

Of course, the repeated refresh will have different values, here just take a more average value.
Win under the Do not have any reference, mainly look at the results of Linux.
Not connent time, probably read and write the speed of APC is faster than the memcached about one times. Count the Memcache_connect time, which is twice times faster.
APC can implement php file opcode cache, also can implement user cache, is really a good thing.

So, if the site size is still small, all functions can be done on a single server, then the caching scheme should be the preferred APC, regardless of memcached. However, if the scale of the site will continue to expand, this time performance differences can be negligible, you should deploy memcached.
In addition, using memcached across servers is a good idea to use the intranet. Otherwise, the memcached will often be connected over time (more than 100ms), and will be twice times more bandwidth-bound by the impact of routing.

http://www.bkjia.com/PHPjc/321268.html www.bkjia.com true http://www.bkjia.com/PHPjc/321268.html techarticle Copy the code as follows: Php $memcachehost = ' localhost '; $memcacheport = ' 11211 '; function microtime_float () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $use ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.