我正在嘗試製作一個腳本來下載文件的一部分。 只需使用CURL和fread進行測試,我知道流傳輸過程中的CURL比fread慢。 爲什麼?如何加快curl流文件? 我不喜歡使用fread,因爲我需要有限的時間在流媒體過程中使用。流文件時CURL比fread慢?如何加快CURL
這是我的示例代碼。
$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.1s
$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();
捲曲需要大約2.5秒。 如果我採取更多步驟來下載更多的文件的一部分。 捲曲會花更慢。
爲什麼捲曲比較慢?它有什麼解決方案?
我已經更新了我的回答如下,它好像你正在比較一個請求與許多不太公平的請求範圍 –