PHP Performance & Benchmarking: PHP remote file access: fopen() and file_get_contents()

Opening files in PHP is easier now because of the file_get_contents() function which is the recommended way to dump a file's contents into a string according to the official PHP documentation. But does this PHP function really perform better than fopen? The following test environment compares the performance of file_get_contents to fopen when retrieving a public Yahoo RSS feed.

Test Environment: Linux, Apache, PHP 5.2.17.

Test Setup: Code constructs listed below. For loops used to have enough iterations to make the script execution time calculatable. Each test was run 25000 times to get the average.

Comparisons

Using fopen(), fread()

$url="http://rss.news.yahoo.com/rss/us";
$data=NULL;
$dataHandle=fopen($url, "r" );
if($dataHandle)
{
	while (!feof($dataHandle))
        {
  		$data.= fread($dataHandle, 4096);
	}
	if($data)
	{
		//DO SOMETHING
	}
	fclose($dataHandle);
}

Using file_get_contents()

$url="http://rss.news.yahoo.com/rss/us";
$data=NULL;
$data=file_get_contents($url);
if($data)
{
	//DO SOMETHING
}
Results
Test Avg Execution Time Comparative Graph - (longer is slower, 100% is the fastest time)
fopen(), fread() 251.51 ms Performance Graph 108%
file_get_contents() 233.86 ms Performance Graph 100%

Curious results! The file_get_contents() function is supposed to be a wrapper for fopen, but the decoupling of the fopen and fread seems to make performance slower.