Skip to main content
Other

file_get_contents() vs readfile(): Which Perform Better?

By October 5, 2013No Comments

Undoubtedly file_get_content(). Now you’re probably ready to criticize that, to say that it’s not possible. I did it too, but listen what I found on a real test.

Premise: this is only a specific case, maybe those result are due to the operative system, the web server, the PHP version, I really don’t know.

Anyway… I was helping to optimize the cache for a site with millions of page views per month. It was an interesting case study since the blog owner moved on my indication from Hyper Cache to Lite Cache (he was needed a specific feature).

During high load periods, Lite Cache was perfoming worse than Hyper Cache state the page was already in cache and even if Lite Cache serving code is lighter than the one of Hyper Cache.

After some digging inside the code, he only real difference was: Lite Cache uses (was using) the readfile() function, while Hyper Cache uses the echo file_get_contents() (actually an equivalent).

So I changed Lite Cache to the second solution and the server load comes down to the same levels or better than we measured using Hyper Cache.

Actually seeing that from outside there are no reasons for that. The readfile() function should be better and use less memory (anyway the overload was a CPU overload). The file_get_contents() uses specific operative system features when available (file memory mapping for example).

I searched the net for real performance test of those functions, but I never find a real test on a real server with simulated high traffic volumes. Of course the server I was optimizing was a production server so I has not much room for experiments.

And now you should have more doubts than before.

 

 

Leave a Reply