日期:2014-04-25  浏览次数:20858 次

置顶的帖子里有一个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 >
下载中...
</body>
</html>