Copy codeThe Code is as follows:
<? Php
$ 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). '<br> ';
$ 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). '<br> ';
$ T1 = microtime_float ();
Apc_store ($ mem_ SQL _key. '_ test', $ arrs, $ cache_time );
Echo "APC_write ==== ";
Echo runtime ($ t1). '<br> ';
$ T1 = microtime_float ();
$ Mem = new Memcache;
$ Mem-> connect ($ memcachehost, $ memcacheport );
Echo "MEM_connet ====". runtime ($ t1). '<br> ';
$ T1 = microtime_float ();
$ Arrs = $ mem-> get ($ mem_ SQL _key );
Echo "MEM_read ==== ";
Echo runtime ($ t1). '<br> ';
$ T1 = microtime_float ();
$ Mem-> set ($ mem_ SQL _key. '_ test', $ arrs, 0, $ cache_time );
Echo "MEM_write ==== ";
Echo runtime ($ t1). '<br> ';
?>
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.