为什么file_get_contents比memcache_get更快? [英] Why is file_get_contents faster than memcache_get?
问题描述
我使用file_get_contents从磁盘加载XML文件,作为一个测试,我发现我可以在3.99秒内使用 file_get_contents()
1,000次加载156K文件。我已经子类化了加载的部分,并用一个memcache层替换了它,在我的开发机器上发现我可以在4.54秒内完成1000个相同文档的加载。
<我明白,file_get_contents()会做一些缓存,但它看起来实际上比知名的缓存技术更快。在单个服务器上,
file_get_contents()
的性能是否可以达到? 我是在PHP 5.2.17通过Macports,OS X 10.6.8。
编辑:我发现这个尺寸的XML文档,有一个小的好处,在使用 MEMCACHE_COMPRESSED
标志。通过内存缓存的1500次加载是在6.44秒(压缩)而不是6.74(没有)的情况下完成的。然而,两者都比 file_get_contents
慢,它在5.71秒内完成相同数量的加载。
因为file_get_contents mmap
这个文件,所以你只会有几个文件系统调用,这将最终在文件系统缓存中。 memcache涉及对memcached的进程外调用(并且在集群实现中离开服务器)。
$ b
file_get_contents()
关键取决于文件系统的类型,例如NFS挂载的文件系统上的文件没有被映射,并且这个访问可能会很慢。同样在多用户服务器上,文件系统缓存可以被其他进程快速刷新,而memcached缓存几乎肯定会在内存中。
I'm loading XML files from disk using file_get_contents, and as a test I find I can load a 156K file using file_get_contents()
1,000 times in 3.99 seconds. I've subclassed the part that does the loading and replaced it with a memcache layer, and on my dev machine find I can do 1000 loads of the same document in 4.54 seconds.
I appreciate that file_get_contents() will do some caching, but it looks like it is actually faster than a well-known caching technique. On a single server, is the performance of file_get_contents()
as good as one can get?
I'm on PHP 5.2.17 via Macports, OS X 10.6.8.
Edit: I've found on XML documents of this size, there is a small benefit to be had in using the MEMCACHE_COMPRESSED
flag. 1,500 loads via memcache are done in 6.44 sec (with compression) rather than 6.74 (without). However both are slower than file_get_contents
, which does the same number of loads in 5.71 sec.
Because file_get_contents mmap
s the file and so you'll only have a few file system calls and this will end up in the file system cache. memcache involves out-of-process calls to the memcached (and out of server on a clustered implementation).
The performance of file_get_contents()
crucially depends on the type of file system, for example a file on an NFS mounted file system is not mmapped and this access can be a LOT slower. Also on a multi-user server, the file system cache can get rapidly flushed by other processes whereas the memcached cache will almost certainly be in memory.
这篇关于为什么file_get_contents比memcache_get更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!