正 文

xmlHTTP返回值重编码的优化


www.7dspace.com  更新日期:2006-1-8 2:49:54  七度空间


置顶的帖子里有一个VBscript的代码用来将xmlHTTP返回值重编码,不过效率极其低下.....俺费了好大的力气,终于做了一个速度勉强让人满意的代码。不过水平所限还是不得不用了vbscript的chr函数用来将acsii转成unicode,不知道哪位大哥能想想办法?
ps:算法优化真是个费脑筋的活。

俺每句话都下了断点测试运行时间,终于把运行时间缩短了大约10倍,现在应该是一种可以实用的算法了。

我一开始用正则替换,每个字替换一遍,结果发现87020字节居然要40秒左右

现在这个代码在我电脑上只需要大约3秒,哈哈哈哈,感觉真爽

注意:对于只需要解码一次的情形本算法不是最优,但是对于需要多次解码的时候应该是最好的算法了。

<html>
<head>
<title>xml Http</title>

<script language=javascript>
//***Author: Hutia

//测试用的网址
url="http:\/\/www.blueidea.com\/";

//确认需要的VBScript函数是否存在,不存在就写一个
try{
if(typeof(rsB)=="undefined"){initVBS();};
}catch(e){initVBS();}

//**全局变量**
//glbEncode储存ascii到unicode字符的转换,这样做的好处是可以提高重复解码时的解码效率
glbEncode=new Array();

//**初始化函数**
function initVBS(){
 //初始化VBscript的两个funtion:rsB 和 vbChar
 strVBS="<script language=vbscript>\r\n";
 strVBS+="Function rsB(vIn)\r\nrsB=MidB(vIn,1)\r\nEnd Function\r\n";
 strVBS+="Function vbChar(ss)\r\nvbChar = Chr(ss)\r\nEnd Function\r\n";
 strVBS+="<\/script>";
 document.write(strVBS);
}

//用同步方式获取网页内容
function loadURL(strURL){
 if(!strURL){return(false);}
 var xmlHTTP=new ActiveXObject("MSXML2.XMLHTTP");
 xmlHTTP.open("GET",strURL,false);
 xmlHTTP.send("");
 return xmlHTTP;
}

//重编码
function reCode(b){
 var s=rsB(b);
 s=escape(s).replace(/%u/gi,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/ig,"%@$1$2");
 var uniS=s.match(/\%@(.{4})/g);
 s=s.replace(/@/g,"");
 if(uniS){
  var k;
  for(var i=0;i<uniS.length;i++){
   k=uniS[i].substring(2,6);
   if(!glbEncode[k]){
    glbEncode[k]=escape(vbChar(eval("0x"+k))).substring(1,6);
   }
  }
  tempA=s.split("%");
  for(var i=0;i<tempA.length;i++){
   if(tempA[i].length!=2){
    tempA[i]=glbEncode[tempA[i]];
   }
  }
  s=tempA.join("%");
 }
 return unescape(s);
}

function init(){
 stTime=(new Date()).getTime();
 ree=loadURL(url).responseBody;
 stTime2=(new Date()).getTime();
 s001=reCode(ree);
 stTime3=(new Date()).getTime();
 s001=reCode(ree);
 stTime4=(new Date()).getTime();
 s001=reCode(ree);
 stTime5=(new Date()).getTime();
 document.body.innerText="共计下载"+s001.length+"字节";
 stTime6=(new Date()).getTime();
 strR="下载用时"+(stTime2-stTime)+"ms\r\n";
 strR+="解码用时"+(stTime3-stTime2)+"ms\r\n";
 strR+="再次解码用时"+(stTime4-stTime3)+"ms\r\n";
 strR+="再次再次解码用时"+(stTime5-stTime4)+"ms\r\n";
 strR+="显示用时"+(stTime6-stTime5)+"ms\r\n";
 document.body.innerText+=strR;
 document.body.innerText+=s001;
}
</script>
</head>
<body onload="init();">
下载中...
</body>
</html>


上一篇:Flash Lite 2.0 新功能介绍
下一篇:Oracle和Oracle认证证书以及报价
作者:hutia  来源:蓝色理想 ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐