Demo code that uses cached APC on the same server is more efficient than Memcached

Source: Internet
Author: User
Tags apc
I have seen some articles saying that the efficiency of APC on the same server is seven times that of Memcached, and the efficiency of APC is higher than that of Memcached. As for how fast it is, I wrote a small program to test it. The code is as follows:


$ Memcachehost = 'localhost ';
$ Memcacheport = '000000 ';
Function microtime_float (){
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}
Function runtime ($ t1 ){
Return number_format (microtime_float ()-$ t1) * 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 );

$ T1 = microtime_float ();
Echo "APC_read ==== ";
$ Arrs = apc_fetch ($ mem_ SQL _key );
Echo runtime ($ t1 ).'
';

$ T1 = microtime_float ();
Apc_store ($ mem_ SQL _key. '_ test', $ arrs, $ cache_time );
Echo "APC_write ==== ";
Echo runtime ($ t1 ).'
';

$ T1 = microtime_float ();
$ Mem = new Memcache;
$ Mem-> connect ($ memcachehost, $ memcacheport );
Echo "MEM_connet ====". runtime ($ t1 ).'
';

$ T1 = microtime_float ();
$ Arrs = $ mem-> get ($ mem_ SQL _key );
Echo "MEM_read ==== ";
Echo runtime ($ t1 ).'
';
$ T1 = microtime_float ();
$ Mem-> set ($ mem_ SQL _key. '_ test', $ arrs, 0, $ cache_time );
Echo "MEM_write ==== ";
Echo runtime ($ t1 ).'
';
?>


The results of this SQL statement are cached in apc and memcached in advance, and then the read/write speed is tested.
Result on windows Server:
Init ==== 0.0341 ms
APC_read ==== 0.0439 ms
APC_write ==== 0.0920 ms
MEM_connet ==== 11.0571 ms
MEM_read ==== 0.2630 ms
MEM_write ==== 0.2270 ms

Result on linux server:
Init ==== 0.0131 ms
APC_read ==== 0.0520 ms
APC_write ==== 0.0489 ms
MEM_connet ==== 0.0501 ms
MEM_read ==== 0.1030 ms
MEM_write ==== 0.0801 ms

Of course, refresh will have different values. here we only get a relatively average value.
Windows does not provide any reference. it mainly depends on the results on linux.
Not counted as the connent time, the read/write speed of apc is about twice faster than that of memcached. Calculate the memcache_connect time, which is twice faster.
APC can cache the opcode of the PHP file or the user cache. this is really a good thing.

Therefore, if all the functions can be completed on one server when the website is small, the first choice for caching is APC, and memcached is not considered. However, if you consider that the website size will continue to expand, the performance difference at this time can be ignored, you should deploy memcached.
In addition, memcached is recommended for cross-server use. Otherwise, memcached often times out (more than 100 ms) due to the impact of routing, and doubles the bandwidth traffic out of thin air.

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.