伤心♂oO○ 发表于 2007 年 7 月 20 日 13:12:43

套套,Come in!

问你个问题,PHP里用什么函数打开远程文件比较快?

wode998 发表于 2007 年 7 月 20 日 13:49:17

come in
如果有次函数的话

姜运涛 发表于 2007 年 7 月 20 日 14:00:15

效率没测试过
但现在都推荐用 file_get_contents 了
不过我还是用 fsockopen 多些

kokgog 发表于 2007 年 7 月 20 日 14:13:16

:( 天真热

卡佩罗 发表于 2007 年 7 月 20 日 14:33:22

这种问题真有趣:lol

姜运涛 发表于 2007 年 7 月 20 日 14:37:25

测试代码

<?php
set_time_limit(0);

function mtime() {
    list($usec, $sec) = explode(' ', microtime());
    return ((float)$usec + (float)$sec);
}

$time_begin = mtime();
for ($i = 0; $i < 50; $i++) {
    $data = '';
    $data = file_get_contents('http://www.baidu.com');
}
$time_end = mtime();
echo 'file_get_contents: ', $time_end - $time_begin, '<br />';

$time_begin = mtime();
for ($i = 0; $i < 50; $i++) {
    $data = '';
    $data = file('http://www.baidu.com');
}
$time_end = mtime();
echo 'file: ', $time_end - $time_begin, '<br />';

$time_begin = mtime();
for ($i = 0; $i < 50; $i++) {
    $data = '';
    $fp = fsockopen('www.baidu.com', 80);
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.baidu.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
      $data .= fgets($fp, 128);
    }
    fclose($fp);
}
$time_end = mtime();
echo 'fsockopen: ', $time_end - $time_begin, '<br />';


第一次
file_get_contents: 12.467694997787
file: 12.547880887985
fsockopen: 9.6251020431519

第二次
file_get_contents: 10.840023040771
file: 12.598785877228
fsockopen: 9.7779731750488

第三次
file_get_contents: 9.7077579498291
file: 9.5748469829559
fsockopen: 12.533878087997

第四次
file_get_contents: 10.059661149979
file: 9.4984700679779
fsockopen: 9.7483789920807

第五次
file_get_contents: 12.61283493042
file: 10.825304985046
fsockopen: 9.6883759498596

姜运涛 发表于 2007 年 7 月 20 日 14:38:26

可见还是 fsockopen 最快
也最烦琐

效率相差不多
不如直接用 file_get_contents

卡佩罗 发表于 2007 年 7 月 20 日 14:43:44

原帖由 姜运涛 于 2007-7-20 14:38 发表 http://www.jgwy.net/bbs/images/common/back.gif
可见还是 fsockopen 最快
也最烦琐

效率相差不多
不如直接用 file_get_contents

其实socket最快,本来file_get_contents读远程文件就是用的socket, 只不过封装了,对外隐藏细节。fsockopen也是:$

卡佩罗 发表于 2007 年 7 月 20 日 14:45:59

 而且楼主估计是采集了,这时候网速决定一切,瓶颈不在php这块。fork出多几个进程可以提高速度.

幽鬼狼魂 发表于 2007 年 7 月 20 日 14:48:11

。。Microsoft.XMLHTTP
客户端执行组件
页: [1] 2 3
查看完整版本: 套套,Come in!