PHP: libcurl VS. file_get_contents

Posted on: October 21, 2006

I’ve proved this in the past, but I suppose it’s time I do it again. Libcurl is generally faster than file_get_contents by a considerable amount.

Many people argue that it’s not any significant amount. This point is used time and time again when comparing functions that don’t access anything except memory, but it doesn’t apply to accessing files over a network.

Having not looked over the source code of libcurl and PHP, I can’t tell you for sure why one is faster than the other, but I can give you proof and some rational thoughts on the matter.

First, libcurl allows you to customize your request a considerable degree. This allows it to lighten up it’s processing on trying to figure out what it’s connecting to and how it should handle everything. It’s opponent is a one-size-fits-all solution, meaning it fits, but not very well. It has to do more work to decide exactly what it should do with each query. There are also a number of headers that could be sent differently between the two that I don’t care to test or look at.

Anyways, here’s the proof you’ve been waiting for: Test

The test runs through a list of websites, connecting 10 times to each using libcurl and file_get_contents and compares the times. Some websites work faster with file_get_contents, and others work faster with libcurl. Occasionally the total will end up within 10 seconds, but I’ve never seen file_get_contents spend less time than libcurl.

As an additional note, even if you are connecting to a website where file_get_contents has been proven to be faster, you should still use libcurl due to it’s error handling abilities. If you’re really opposed to libcurl or can’t use it, use sockets. 🙂


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: