Benchmarking file_get_contents and cURL. whose faster ?

Every developer always wants to make their applications faster. In world of programming, there are always some better alternative ways to do things. The faster your application executes, the better.

People rather get confused on which alternative to use between file_get_contents() and cURL, which one is faster ? Let’s do some quick benchmarking to realize..we will try to fetch www.google.com by using file_get_contents and cURL both.

<?php
	$url = 'http://www.google.com/';
	echo "\nfile_get_contents:\n";
	for ($i = 0; $i < 10; $i++)
	{
        $start = microtime(true);
        $data = file_get_contents($url);
        echo microtime(true) - $start, "\n";
	}
	echo "cURL:\n";
	for ($i = 0; $i < 10; $i++)
	{
        $start = microtime(true);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 10);
        $data = curl_exec($ch);
        curl_close($ch);
        echo microtime(true) - $start, "\n";
	}
?>

Output :

file_get_contents:
0.33223700523376
0.32492804527283
0.29478597640991
0.28198003768921
0.2946789264679
0.29429292678833
0.29543614387512
0.29854202270508
0.28568601608276
0.29483699798584
cURL:
0.1754047870636
0.15225791931152
0.14072608947754
0.15246987342834
0.15354514122009
0.15439510345459
0.15201306343079
0.140380859375
0.15269589424133
0.15341806411743

Hence proved, cURL is much faster than file_get_contents(). So next time you want better execution speed, always use cURL.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>