PHP学习笔记,curl,file_get_content,include和fopen四种方法获取远程文件速度测试.

这几天在作抓取.发现用PHP的file_get_contents函数来获取远程文件的过程当中老是出现失败,而且效率很低下.因此就作了个测试的demo来测试下PHP中各类方法获取文件的速度.php

程序里面使用了四种方法   分别是curl

1,使用输入输出缓冲和include包含远程文件拿到对应url的内容函数

这个须要开启PHP的allow_url_include选项测试

2,使用fopen来以只读的方式打开并读取远程文件.url

3,使用file_get_contents函数来获取远程url文件.spa

4,使用PHP的curl拓展来获取远程文件.blog

具体里面是啥工做原理我不知道,不过经过测试我获得的结果是get

第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
it

 

 

第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("&nbsp::::<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("&nbsp ::::<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("&nbsp ::::<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("&nbsp ::::<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,明显是后者快一些.

相关文章
相关标签/搜索