Why is file_get_contents faster than memcache_get?

I load XML files from disk using file_get_contents, and as a test I can load a 156K file using file_get_contents() 1,000 times in 3.99 seconds. I have subclassed the part that loads and replaces it with the memcache level, and on my machine computer will find 1000 downloads 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 the well-known caching method. On one server, the performance of file_get_contents() no worse than what you can get?

I am on PHP 5.2.17 through Macports, OS X 10.6.8.

Edit: I found in XML documents of this size, there is a slight advantage in using the MEMCACHE_COMPRESSED flag. 1,500 downloads via memcache are performed after 6.44 s (with compression), and not 6.74 (without). However, they are slower than file_get_contents , which does the same amount of load in 5.71 seconds.

+8
php file-io php-internals memcached
source share
3 answers

Because file_get_contents is a mmap file, and therefore you will only have a few system calls, and this will end in the file system cache. memcache includes out-of-process calls for memcached (and off-server calls in a clustered implementation).

The performance of file_get_contents() is critically dependent on the type of file system, for example, a file in an NFS file system is not marked, and this access may be slower. In addition, on a multi-user server, the file system cache can be quickly flushed by other processes, while the memcached cache will almost certainly be in memory.

+8
source share

file_get_contents is the easiest way to get a file. The main operating system (especially Linux) already has efficient caching mechanisms. Everything you do just creates overhead and slows down.

Memcache makes sense if you download these files from a remote location.

Edit: It is not necessarily true that file_get_contents is the easiest way. fopen / fget can be even faster - I don't know. But the differences should be small compared to the complexity of the cache layer.

+3
source share

Saving XML files in memcache is very small for me.

I would rather keep parsed values, while preserving both reading and parsing.

+1
source share

All Articles