ASP中用Join和Array,可以加快字符连接速度。-ASP技术-3P代码网
繁体中文
设为首页
加入收藏
当前位置:ASP技术首页 >> ASP技巧 >> ASP中用Join和Array,可以加快字符连接速度。

ASP中用Join和Array,可以加快字符连接速度。

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:29  文字大小:【】【】【
简介:比如 <% 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...

比如

<%

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"要快很多

详细测试数据可以看:

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

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

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

New Document

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

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

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

//--------------------------------------------------

//test-js.asp

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

//--------------------------------------------------

//--------------------------------------------------

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

//--------------------------------------------------

//test-js2.asp

//--------------------------------------------------

//--------------------------------------------------

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

//--------------------------------------------------

//test-js3.asp

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

//--------------------------------------------------

//--------------------------------------------------

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

//--------------------------------------------------

//test-js3.asp

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

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

//--------------------------------------------------

//--------------------------------------------------

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

//--------------------------------------------------

//test-js5.asp

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

//建立零时文件所用的组件是Adodb.Stream

//--------------------------------------------------

//--------------------------------------------------

//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 "
" & 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 "
"&(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 "
"&(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 "
"&(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 "
"&(timer-t)

%>

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

{以下是测试结果}

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

[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("
".(($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("
".(($now["sec"]-$t["sec"])*1000000+$now["usec"]-$t["usec"]));

?>

//--------------------------------------------------

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

[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函数连接起来

//--------------------------------------------------

//--------------------------------------------------

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

//--------------------------------------------------

//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 "
" & CSTR(timer-t)

%>

//--------------------------------------------------

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

//--------------------------------------------------

[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中确实有很大影响,但不影响它的成绩

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

}

责任编辑:admin
相关文章