php fread 慢,php – 当流媒体文件时,CURL比fread慢?如何加速CURL

我正在尝试制作一个脚本来下载文件的一部分.

只需用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左右.

如果我采取更多步骤下载更多的文件的一部分.

卷曲会慢一些.

为什么卷曲更慢?它是什么解决方案?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值