PHP学习笔记,curl,file_get_content,include和fopen四种方法获取远程文件速度测试.
2013-08-29 18:59
1081 查看
这几天在做抓取.发现用PHP的file_get_contents函数来获取远程文件的过程中总是出现失败,并且效率很低下.所以就做了个测试的demo来测试下PHP中各种方法获取文件的速度.
程序里面使用了四种方法 分别是
1,使用输入输出缓冲和include包含远程文件拿到对应url的内容
这个需要开启PHP的allow_url_include选项
2,使用fopen来以只读的方式打开并读取远程文件.
3,使用file_get_contents函数来获取远程url文件.
4,使用PHP的curl拓展来获取远程文件.
具体里面是啥工作原理我不知道,不过通过测试我得到的结果是
第100次调用:get_file_by_curl:used_time ::::0.0732s
100次平均时间:0.084043
失败次数:0
第100次调用:get_file_by_file_get_contents:used_time ::::0.103s
100次平均时间:0.11445643564356
失败次数:0
第100次调用:get_file_by_fopen:used_time ::::0.0905s
100次平均时间:0.086212871287129
失败次数:0
第100次调用:get_file_by_include:used_time ::::0.1248s
100次平均时间:0.11332079207921
失败次数:0
这上面是通过100次请求博客园首页的文件得出的结果 数据不多,但是还是能看出来区别的 用file_get_contents和include+缓冲区这两种方法的速度明显要比curl和fopen两种方式慢
下面是测试代码
这个测试的结果还不能够说明什么,我打算再找机会好好测试下.这里我有几个地方不太理解,之前有人也做过类似的测试 只测试了file_get_content和curl,明显是后者快一些.
程序里面使用了四种方法 分别是
1,使用输入输出缓冲和include包含远程文件拿到对应url的内容
这个需要开启PHP的allow_url_include选项
2,使用fopen来以只读的方式打开并读取远程文件.
3,使用file_get_contents函数来获取远程url文件.
4,使用PHP的curl拓展来获取远程文件.
具体里面是啥工作原理我不知道,不过通过测试我得到的结果是
第100次调用:get_file_by_curl:used_time ::::0.0732s
100次平均时间:0.084043
失败次数:0
第100次调用:get_file_by_file_get_contents:used_time ::::0.103s
100次平均时间:0.11445643564356
失败次数:0
第100次调用:get_file_by_fopen:used_time ::::0.0905s
100次平均时间:0.086212871287129
失败次数:0
第100次调用:get_file_by_include:used_time ::::0.1248s
100次平均时间:0.11332079207921
失败次数:0
这上面是通过100次请求博客园首页的文件得出的结果 数据不多,但是还是能看出来区别的 用file_get_contents和include+缓冲区这两种方法的速度明显要比curl和fopen两种方式慢
下面是测试代码
<?php /** *名称:远程获取文件测试 *作用:测试各种方法获取远程文件的速度 *作者:swordphp@126.com *创建时间:2013-08-29 *最后修改时间:2013-08-29 **/ ini_set("max_execution_time", "0"); set_time_limit(0); ini_set("error_reporting", "E_ALL & ~E_NOTICE"); ini_set("allow_url_include",1); class fileget_test{ public function __construct(){ } //通过CURL拓展获取文件内容 public function get_file_by_curl($url){ echo "get_file_by_curl:used_time"; $start_time = microtime(true); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);//设置curl地址 curl_setopt($ch, CURLOPT_TIMEOUT, 5);//设置超时时间. curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); curl_setopt($ch, CURLOPT_REFERER,_REFERER_); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); $used_time = round((microtime(true)-$start_time),4); echo nl2br(" ::::<span style=\"color:red\";>".$used_time."s\n\r</span>"); return $used_time; } //通过file_get_content来获取文件内容 public function get_file_by_file_get_contents($url){ echo "get_file_by_file_get_contents:used_time"; $start_time = microtime(true); $content = file_get_contents($url); $used_time = round((microtime(true)-$start_time),4); echo nl2br("  ::::<span style=\"color:red\";>".$used_time."s\n\r</span>"); return $used_time; } //通过fopen来获取文件内容 public function get_file_by_fopen($url){ echo "get_file_by_fopen:used_time"; $start_time = microtime(true); $handle = fopen($url,'r'); $content = fread($handle, 100000); fclose($handle); $used_time = round((microtime(true)-$start_time),4); echo nl2br("  ::::<span style=\"color:red\";>".$used_time."s\n\r</span>"); return $used_time; } //通过include获取远程文件 public function get_file_by_include($url){ echo "get_file_by_include:used_time"; $start_time = microtime(true); ob_start(); include($url); $coutent = ob_get_contents(); ob_clean(); $used_time = round((microtime(true)-$start_time),4); echo nl2br("  ::::<span style=\"color:red\";>".$used_time."s\n\r</span>"); return $used_time; } } function my_test($function,$url){ $res = array(); $test = new fileget_test; switch ($function) { case 'get_file_by_curl': for($i=1;$i<=100;$i++){ echo "第".$i."次调用:"; $res[$i] = $test ->get_file_by_curl($url); } echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r"); echo "失败次数:".count(array_keys($res,false)); break; case 'get_file_by_file_get_contents': for($i=0;$i<=100;$i++){ echo "第".$i."次调用:"; $res[$i] = $test ->get_file_by_file_get_contents($url); } echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r"); echo "失败次数:".count(array_keys($res,false)); break; case 'get_file_by_fopen': for($i=0;$i<=100;$i++){ echo "第".$i."次调用:"; $res[$i] = $test ->get_file_by_fopen($url); } echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r"); echo "失败次数:".count(array_keys($res,false)); break; case 'get_file_by_include': for($i=0;$i<=100;$i++){ echo "第".$i."次调用:"; $res[$i] = $test ->get_file_by_include($url); } echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r"); echo "失败次数:".count(array_keys($res,false)); break; default: echo "no function selected!"; break; } } $function = $_GET['f']; $url = isset($_GET['url'])?$_GET['url']:'http://www.taobao.com'; my_test($function,$url);
这个测试的结果还不能够说明什么,我打算再找机会好好测试下.这里我有几个地方不太理解,之前有人也做过类似的测试 只测试了file_get_content和curl,明显是后者快一些.
相关文章推荐
- PHP下通过file_get_contents\curl的方法实现获取远程网页内容(别忘了还有PhpRPC)
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- file_get_contents是打工文件或URL获取内容的方法,比其稳定的还有curl_get_contents
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件curl,fsockopen,file_get_contents,file,fopen几种方法
- PHP下通过file_get_contents()方法不能正常获取远程网页内容
- php远程获取图片或文件信息(get_headers,fsocketopen,curl)
- php 两种方法获取url内容 file_get_contents和curl
- php读取网络文件(curl, fsockopen ,file_get_contents 几个方法的效率对比)
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- PHP使用file_get_content设置头信息的方法
- php学习笔记6--php中的文件包含 include,require,include_once,require_once
- PHP中获取远程文件的三种方法
- curl 或 file_get_contents 获取需要授权页面的方法
- 如何在mapreduce方法中获取当前使用文件(get file name)
- 带超时+POST/GET方式的获取远程文件,利用file_get_contents
- zstack学习笔记1--HEX file content at address 0x4A390 exceeds chip's 256 kB flash size错误解决方法
- PHP使用get_headers函数判断远程文件是否存在的方法