比如
<%
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="Descr</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)+"/n";
a[i]=String(i);
}
s=a.join("/n");
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)+"/n";
}
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+"/n");
}
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+"/n");
}
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."/n";
}
echo($s."/n");
$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."/n");
}
$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("/n",$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."/n");
}
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("/n");
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中確實有很大影響,但不影響它的成績
所以,不必為不斷擴展數組擔心
}
新聞熱點
疑難解答