ウェブ上のコンテンツを取得する際、cURLとfreadではどちらが早いのか気になったので調べてみた。
結果はstackoverflowで見つけたのでペタリ。
■fread
$start = microtime(true);
$f = fopen('http://example.com','r');
$response = fread($f, 3);
echo $response;
$response = fread($f, 3);
echo $response;
$response = fread($f, 3);
echo $response;
$response = fread($f, 3);
echo $response;
$response = fread($f, 3);
echo $response;
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();
■cURL
$start = microtime(true);
$curl = curl_init('http://example.com');
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_RANGE, "0-2");
$response = curl_exec($curl);
echo $response;
curl_setopt($curl, CURLOPT_RANGE, "3-5");
$response = curl_exec($curl);
echo $response;
curl_setopt($curl, CURLOPT_RANGE, "6-8");
$response = curl_exec($curl);
echo $response;
curl_setopt($curl, CURLOPT_RANGE, "9-11");
$response = curl_exec($curl);
echo $response;
curl_setopt($curl, CURLOPT_RANGE, "12-14");
$response = curl_exec($curl);
echo $response;
curl_close($curl);
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();
■result
・fread
1.1 seconds
・cURL
2.5 seconds
■結論
freadのほうが早い模様。
cakePHPのcoreでもcURLじゃなくてfreadが使われてるし、こちらのほうがパフォーマンスに優れているのかな?