Mehdi Rahimi Mehdi Rahimi - 2 months ago 17
PHP Question

loadHTML & loadHTMLFile, which one is faster?

Between these two, which one is faster? Getting content directly by

loadHTMLFile
or first by
cURL
and then pass to
loadHTML




$doc = new DOMDocument();
@$doc->loadHTMLFile($url);


Or



$doc = new DOMDocument();
$ch = curl_init();
$timeout = 500;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
@$doc->loadHTML($data);

Answer

Add as the last line of your script and try each method at least 3 times to get an accurate idea of which is fastest.

Page generated in 
<?php echo  round((microtime(TRUE)-$_SERVER['REQUEST_TIME_FLOAT']), 4); ?>
 seconds on <?php echo date("M jS Y"); ?>
Comments