欢迎光临
我们一直在努力

ASP中用Join和Array,可以加快字符连接速度。-ASP教程,ASP技巧

建站超值云服务器,限时71元/月

比如

<%

dim a(10000),i,t

t=timer

for i=0 to 10000

a(i)=cstr(i)

next

response.write join(a,vbcrlf)

response.write timer-t

erase a

%>

速度可以和php一拼(虽然还是没有他快)

另一种用法是

s=join(array("1","2","3",…..,"9999"))

速度依然比"1" & "2" & "3" & …..& "9999"要快很多

详细测试数据可以看:

////////////////////////////////////////////////////

//{测试用的客户端模版}

////////////////////////////////////////////////////

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title> new document </title>

<meta name="generator" content="editplus">

<meta name="author" content="">

<meta name="keywords" content="">

<meta name="description" content="">

</head>

<body>

<script language="vbscript">

dim t

t=timer

</script>

<!–这儿放服务器测试脚本–>

<script language="vbscript">

document.write "|" & (timer-t) 输出客户端完全接受到所有数据所用的时间

</script>

</body>

</html>

////////////////////////////////////////////////////

//{测试的各个脚本的代码}

////////////////////////////////////////////////////

//————————————————–

//test-js.asp

//使用数组收集所有的字符窜,最后通过join函数连接起来

//————————————————–

<script language="javascript" runat="server">

var i,t,s;

var a=new array(10000);

t=(new date()).gettime();

for(i=0;i<10000;i++){

//s+=string(i)+" ";

a[i]=string(i);

}

s=a.join(" ");

response.write(s);

response.write("<br>"+string((new date()).gettime()-t));

a=null;

s=null;

</script>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-js2.asp

//————————————————–

<script language="javascript" runat="server">

var i,t,s="";

t=(new date()).gettime();

for(i=0;i<10000;i++){

s+=string(i)+" ";

}

response.write(s);

response.write((new date()).gettime()-t);

a=null;

s=null;

</script>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-js3.asp

//每得到一个数据,立刻输出到数据流中

//————————————————–

<script language="javascript" runat="server">

var i,t;

t=(new date()).gettime();

for(i=0;i<10000;i++){

response.write(i+" ");

}

response.write("<br>");

response.write((new date()).gettime()-t);

</script>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-js3.asp

//这个程序通过建立零时文件,并将所有内容输入到文件中,最后统一输出

//建立零时文件所用的组件是fso

//————————————————–

<script language="javascript" runat="server">

var i,t;

t=(new date()).gettime();

var fso=server.createobject("scripting.filesystemobject");//建立fso对象

var f=fso.createtextfile(server.mappath("temp.txt"),true);//通过fso对象创建一个零时文件

for(i=0;i<10000;i++){

f.writeline(i);

}

f.close();

f=fso.opentextfile(server.mappath("temp.txt"),1);

response.write(f.readall());//读出零时文件的内容

f.close();

f=null;

fso=null;

response.write("<br>");

response.write((new date()).gettime()-t);

</script>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-js5.asp

//这个程序通过建立零时文件,并将所有内容输入到文件中,最后统一输出

//建立零时文件所用的组件是adodb.stream

//————————————————–

<script language="javascript" runat="server">

var i,t;

t=(new date()).gettime();

var ado=server.createobject("adodb.stream");

ado.mode=3;//设置为可读可写

ado.type=2;//设置内容为文本

ado.open();

for(i=0;i<10000;i++){

ado.writetext(i+" ");

}

ado.savetofile(server.mappath("temp.txt"),2);//保存一下,才可以读取

response.write(ado.readtext(-1));

ado.close();

ado=null;

response.write("<br>");

response.write((new date()).gettime()-t);

</script>

//————————————————–

//test-vbs.asp

//这个程序使用数组收集所有的字符窜,最后通过join函数连接起来

//对应于test-js.asp

//————————————————–

<%

dim i,a(9999),t

t=timer

for i=0 to 9999

a(i)=cstr(i)

next

s=join(a,vbcrlf)

response.write s

response.write "<br>" & cstr(timer-t)

erase a

s=""

%>

//————————————————–

//test-vbs2.asp

//使用一个零时的字符窜变量收集内容,最后输出

//对应于test-js2.asp

//————————————————–

<%

dim i,j,s,t

t=timer

for i=0 to 9999

s=s & cstr(i) & vbcrlf

next

response.write s

s=""

response.write "<br>"&(timer-t)

%>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-vbs3.asp

//每得到一个数据,立刻输出到数据流中

//————————————————–

<%

dim i,j,s,t

t=timer

for i=0 to 9999

response.write cstr(i) & vbcrlf

next

response.write "<br>"&(timer-t)

%>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-vbs4.asp

//这个程序通过建立零时文件,并将所有内容输入到文件中,最后统一输出

//建立零时文件所用的组件是fso

//对应于test-js4.asp

//————————————————–

<%

dim i,t,fso,f

t=timer

set fso=server.createobject("scripting.filesystemobject")

set f=fso.createtextfile(server.mappath("temp.txt"),true)

for i=0 to 9999

f.writeline cstr(i)

next

f.close

set f=fso.opentextfile(server.mappath("temp.txt"),1)

response.write f.readall

f.close

set f=nothing

set fso=nothing

response.write "<br>"&(timer-t)

%>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-vbs5.asp

//这个程序通过建立零时文件,并将所有内容输入到文件中,最后统一输出

//建立零时文件所用的组件是adodb.stream

//对应于test-js5.asp

//————————————————–

<%

dim i,t,ado

t=timer

set ado=server.createobject("adodb.stream")

ado.mode=3设置为可读可写

ado.type=2设置内容为文本

ado.open

for i=0 to 9999

ado.writetext cstr(i)&vbcrlf

next

ado.savetofile server.mappath("temp.txt"),2 保存一下,才可以读取

response.write ado.readtext()读出全部内容,写入传送流

ado.close

set ado=nothing

response.write "<br>"&(timer-t)

%>

{测试数据统一使用0到9999的一万个数据,每个数据后追加一个回车,通过各种途径输出到客户端屏幕,得出所需时间}

{以下是测试结果}

{测试结果的格式:服务器段测试结果|客户端测试结果}

<celeron 466mhz 256mb sdram>

[windows98se pws 4.0]

[internetexplorer 6.0 service park 1]

//test-js.asp(单位:毫秒|秒)

//asp using javascript and array join

{javascript使用数组收集每一个测试数据,最后用join连接并输出,速度非常快}

390 |.0546875

440 |.0546875

490 |0

380 |0

440 |.046875

430 |.109375

440 |0

440 |.0625

440 |.046875

490 |.109375

440 |.0546875

////////////////////////////////////////////////////

//test-js2.asp(单位:毫秒|秒)

//asp using javascript and temperory sting join

{javascript使用零时字符串收集每一个测试数据,最后并输出}

{速度比较慢,页面出现前,都有短暂的等待,但是和vbscript快了很多}

4290 |0

3680 |.046875

4000 |0

3570 |.0625

3960 |.0546875

4070 |0

4290 |.0546875

4010 |.046875

3740 |0

4780 |0

4070 |.046875

4120 |.046875

////////////////////////////////////////////////////

//test-js3.asp(单位:毫秒|秒)

//asp using javascript and directly output

{javascript每得到一个测试数据便立即输出}

{速度比js用零时字符串速度要慢,但比vbscript直接输出快一点}

{但十分奇怪的是,客户端的运行时间几乎一直是0}

6700 |0

6750 |0

6920 |0

6650 |0

6650 |.046875

6650 |0

6920 |0

6970 |.0546875

6920 |0

7090 |0

////////////////////////////////////////////////////

//test-js4.asp(单位:毫秒|秒)

//asp using javascript and temperoy file with fso

{javascript使用fso建立零时缓冲文件}

{速度很快,但比数组连接慢}

600 |.0625

600 |0

660 |0

660 |.0625

660 |0

660 |.0546875

660 |0

720 |0

660 |0

660 |.0625

////////////////////////////////////////////////////

//test-js5.asp(单位:毫秒|秒)

//asp using javascript and temperoy file with adodb.stream

{javascript使用adodb.stream建立零时缓冲文件}

{速度很快,比javascript的其他方法都快,但比vbscript的数组连接要慢}

380 |.0625

330 |0

390 |0

380 |.0625

390 |.0546875

390 |0

390 |.046875

390 |.0546875

380 |.0625

390 |.046875

////////////////////////////////////////////////////

//test-vbs.asp(单位:秒|秒)

//asp using vbscript and array join

{vbscript使用数组收集每一个测试数据,最后用join连接并输出}

{速度是asp测试中,速度最快的}

.171875 |.3828125

.1640625|.546875

.1640625|.3828125

.2265625|.328125

.21875 |.390625

.21875 |.375

.171875 |.328125

.2265625|.3828125

.21875 |.3828125

.21875 |.3359375

.21875 |.328125

////////////////////////////////////////////////////

//test-vbs2.asp(单位:秒|秒)

//asp using vbscript and temperory string join

{vbscript使用零时字符串收集每一个测试数据,最后输出}

{速度是asp测试中,速度最慢的,javascript中同样的方法也只有这个的一半都不到}

{其原因也许在于字符串连接和s=s&"x"的不合理赋值方式}

10.71094 |10.75781

10.71094 |10.875

9.945313 |10.05469

9.773438 |9.882813

10.16406 |10.32031

10.21875 |10.32813

10.10156 |10.21094

9.671875 |9.78125

9.945313 |10.10938

9.9375 |10.10938

9.945313 |10.05469

////////////////////////////////////////////////////

//test-vbs3.asp(单位:秒|秒)

//asp using vbscript and directly output

{vbscript每得到一个测试数据便立即输出}

{速度是asp测试中,速度仅比vbscript的零时字符连接快,和javascript的同种方法得出的结果差不多,略慢}

7.46875 |7.421875

7.296875 |7.296875

7.03125 |7.03125

7.359375 |7.359375

7.3125 |7.3125

7.359375 |7.359375

7.1875 |7.1875

7.25 |7.25

7.304688 |7.304688

7.1875 |7.1875

7.640625 |7.640625

////////////////////////////////////////////////////

//test-vbs4.asp(单位:秒|秒)

//asp using vbscript and temperoy file with fso

{vbscript使用fso建立零时缓冲文件}

{速度很快,但比javascript的同种方法略慢}

.828125 |1.046875

.765625 |.9296875

.828125 |1.039063

.71875 |.828125

.7109375|.875

.71875 |.828125

.71875 |.8828125

.71875 |.828125

.7734375|.8671875

.7734375|.8203125

////////////////////////////////////////////////////

//test-vbs5.asp(单位:秒|秒)

//asp using vbscript and temperoy file with adodb.stream

{vbscript使用fso建立零时缓冲文件}

{速度很快,和javascript的同种方法结果相近}

.390625 |.6015625

.59375 |.765625

.4921875|.6484375

.3828125|.546875

.3359375|.5546875

.328125 |.546875

.390625 |.5

.3359375|.4921875

.390625 |.5

.3359375|.5

////////////////////////////////////////////////////

//总结

////////////////////////////////////////////////////

{

测试结果很明显,数组连接及用adodb.stream建立缓冲文件在速度上占了上风

服务器端使用javascript会使客户端的反应加快?!!??

性能上vbscript在数组连接上性能很好

其他的不如javascript

但数组连接及用adodb.stream建立缓冲文件这两者各有缺陷

i 对于数组连接,用法比较复杂

1.数组连接在实际运用中,必须设置一个指针变量

2.使用上,由于数组的大小是在变化的

a.对于javascript,

必须用"var arrtemp=new array();"来声明,这样可以不断扩大数组的尺寸

b.对于vbscript

必须用dim arrtemp()来声明,并在程序用使用redim preserve arrtemp(p+size)来扩大数组的尺寸,preserve是用来保留数组中原有的内容

ii对于adodb.stream建立缓冲文件的方法

我们必须设置一个零时文件,但每调用一次页面都要写这个文件,如果使用同一个零时文件,这就容易出现冲突

可以使用当前的时间来做零时文件名,以减少冲突,或者给文件作一个标示,如果文件没有过期,便直接读取,过期了,便打开写入新的内容

后者比较合适,前者容易造成零时文件的泛滥,垃圾成堆

但是后者实现也很复杂

此外,我没测试内存使用情况,不知道数组连接对内存使用会造成多大影响

}

////////////////////////////////////////////////////

//附1

////////////////////////////////////////////////////

{下面是用于对照的php脚本}

////////////////////////////////////////////////////

//————————————————–

//test.php

//使用字符连接

//对应于test-js2.asp和test-vbs2.asp

//————————————————–

<?

$t=gettimeofday();

for($i=0;$i<10000;$i++){

$s.=$i." ";

}

echo($s." ");

$now=gettimeofday();

echo(($now["sec"]-$t["sec"])*1000000+$now["usec"]-$t["usec"]);

?>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test2.php

//直接输出

//对应于test-js3.asp和test-vbs3.asp

//————————————————–

<?

$t=gettimeofday();

for($i=0;$i<10000;$i++){

echo($i." ");

}

$now=gettimeofday();

echo(($now["sec"]-$t["sec"])*1000000+$now["usec"]-$t["usec"]);

?>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test3.php

//使用数组连接

//对应于test-js.asp和test-vbs.asp

//————————————————–

<?

$t=gettimeofday();

for($i=0;$i<10000;$i++){

$s[$i]=$i;

}

echo(implode(" ",$s));

$now=gettimeofday();

echo("<br>".(($now["sec"]-$t["sec"])*1000000+$now["usec"]-$t["usec"]));

?>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test4.php

//使用零时文件

//对应于test-js4.asp,test-js5.asp,test-vbs4.asp和test-vbs5.asp

//————————————————–

<?

$t=gettimeofday();

$fp=fopen("temp.txt","w");

for($i=0;$i<10000;$i++){

fwrite($fp,$i." ");

}

fclose($fp);

//readfile("temp.txt");

include("temp.txt");

$now=gettimeofday();

echo("<br>".(($now["sec"]-$t["sec"])*1000000+$now["usec"]-$t["usec"]));

?>

//————————————————–

////////////////////////////////////////////////////

<celeron 466mhz 256mb sdram>

[windows98se apache]

[internetexplorer 6.0 service park 1]

//test.php(单位:微秒|秒)

//php temperory string join

{快}

188517|.109375

204281|.21875

174301|.171875

179169|.171875

185047|.1679688

198225|.1679688

200802|.1601563

217518|.2226563

199039|.171875

178899|.109375

////////////////////////////////////////////////////

//test2.php(单位:微秒|秒)

//php directly output

{也很快}

197242|.2226563

241610|.2695313

227355|.2734375

214959|.2226563

210478|.21875

230015|.2226563

222359|.1601563

215845|.21875

226364|.21875

210501|.21875

////////////////////////////////////////////////////

//test2.php(单位:微秒|秒)

//php using array join

{前面得很快便输出了,但到倒数几个(从9939开始)等待了很长时间}

{不知道是不是我的机子的问题,也许和apache服务器的程序设计有关}

358020|23.01172

340309|22.1875

397571|22.46875

365696|21.64063

495641|23.57031

464867|34.71094

530083|27.41406

493962|26.03125

351829|26.40625

430496|26.08594

////////////////////////////////////////////////////

//test4.php(单位:微秒|秒)

//php using temperory file

{依然很快}

215117|.171875

220059|.171875

231748|.1640625

211022|.109375

232915|.1640625

196025|.1640625

210776|.21875

217552|.1640625

216197|.171875

259508|.171875

////////////////////////////////////////////////////

//总结

////////////////////////////////////////////////////

{

php的速度还是….

根本不用为速度而担忧,可以使用任何一种方式

asp我也没话好说的,总算数组连接方式还可以和php一拼,不过很奇怪,asp with javascript 为什么在客户端的测试结果那么好?!

不清楚….

}

////////////////////////////////////////////////////

//附2:数组不断扩展的测试

////////////////////////////////////////////////////

//————————————————–

//test-js6.asp

//使用数组收集所有的字符窜,最后通过join函数连接起来

//————————————————–

<script language="javascript" runat="server">

var i,t,s;

var a=new array();

t=(new date()).gettime();

for(i=0;i<10000;i++){

a[i]=string(i);

}

s=a.join(" ");

response.write(s);

response.write("<br>"+string((new date()).gettime()-t));

a=null;

s=null;

</script>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

//test-vbs6.asp

//使用数组收集所有的字符窜,最后通过join函数连接起来

//————————————————–

<%

dim i,a(),t

t=timer

for i=0 to 9999

redim preserve a(i) 重定义大小

a(i)=cstr(i)

next

response.write join(a,vbcrlf)

erase a

response.write "<br>" & cstr(timer-t)

%>

//————————————————–

////////////////////////////////////////////////////

//————————————————–

<celeron 466mhz 256mb sdram>

[windows98se pws 4.0]

[internetexplorer 6.0 service park 1]

//test-js6.asp(单位:毫秒|秒)

//asp using javascript and array join

{javascript使用数组收集每一个测试数据,最后用join连接并输出,但初始化时不直接给其指定大小}

{速度还是很快}

650 |0

440 |.046875

440 |.0625

440 |0

440 |.0546875

440 |.109375

490 |0

440 |.0546875

440 |0

////////////////////////////////////////////////////

//test-vbs6.asp(单位:毫秒|秒)

//asp using vbscript and array join

{vbscript使用数组收集每一个测试数据,最后用join连接并输出,但初始化时不直接给其指定大小,程序中通过redim preserve重新定义}

{速度还是很快}

.328125 |.28125

.328125 |.5

.328125 |.5

.3828125|.3828125

.328125 |.4375

.328125 |.390625

.328125 |.4375

.3359375|.390625

.3359375|.4453125

.390625 |.390625

.3359375|.4921875

.390625 |.3828125

////////////////////////////////////////////////////

//总结

////////////////////////////////////////////////////

{

不断扩展数组,在javascript中对性能的影响并不是很大

在vbscript中确实有很大影响,但不影响它的成绩

所以,不必为不断扩展数组担心

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP中用Join和Array,可以加快字符连接速度。-ASP教程,ASP技巧
分享到: 更多 (0)

相关推荐

  • 暂无文章