我正在尝试制作一个脚本来下载文件的一部分.
只需用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.'
';
$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();
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.'
';
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();
卷曲需要2.5s左右.
如果我采取更多步骤下载更多的文件的一部分.
卷曲会慢一些.
为什么卷曲更慢?它是什么解决方案?