ファイルの一部をダウンロードするスクリプトを作成しようとしています。CURL と fread でテストを行うだけで、ストリーミング プロセス中の CURL は fread よりも遅いことがわかりました。なんで?ファイルをストリームするためにcurlを高速化する方法は? ストリーミング処理中に限られた時間を必要とするため、 fread 、 fopen を使用したくありません。
これが私のサンプルコードです。
$start = microtime(true);
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();
fread / fopen わずか1.1秒
$start = microtime(true);
$curl = curl_init('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg');
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.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "3-5");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "6-8");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "9-11");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "12-14");
$response = curl_exec($curl);echo $response.'<br>';
curl_close($curl);
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();
curl には約 2.5 秒かかりました。ファイルのより多くの部分をダウンロードするためにさらに一歩踏み出すと。curl はさらに遅くなります。
curl が遅いのはなぜですか? そしてそれはどのような解決策ですか?