字符串连接性能测试

先看一个例子:

<%
Dim StartTime,EndTime,i
Dim Temp,Arr(4999)

Const a1="aaaaaa"
Const a2="bbbbbb"
Const a3="cccccc"

StartTime = Timer()
'逐个打开测试
'Response.Write(Test1())
'Response.Write(Test2())
'Response.Write(Test3())
EndTime = Timer()
Response.Write("使用时间:" & FormatNumber((EndTime-StartTime),3) & "毫秒")

Function Test1()
    For i=0 to 4999
        Temp = Temp & a1 & a2 & a3
    Next
    Test1 = Temp
End Function

Function Test2()
    For i=0 to 4999
        Temp = Temp & (a1 & a2 & a3)
    Next
    Test2 = Temp
End Function

Function Test3()
    For i=0 to 4999
        Arr(i) = a1 & a2 & a3
    Next
    Test3 = Join(Arr,"")
End Function
%>

测试结果:

Test1平均使用时间:0.594毫秒
Test2平均使用时间:0.125毫秒
Test3平均使用时间:0.016毫秒

不用再说什么了,从结果中我们已经能清楚的知道哪种性能最好了,呵呵。

参考文章:

1.VBscript 字符串串接的优化技巧:http://bbs.dvbbs.net/dispbbs.asp?boardID=148&ID=1445441
2.关于vbs的字符串操作效率分析总结:http://bbs.dvbbs.net/dispbbs.asp?boardid=1&Id=1479595

评论: 2 | 引用: 0 | 查看次数: 4155
dnawo[2008-02-12 08:52 PM | | | 220.249.163.17 | del | 回复回复]
板凳
是配置的原因吧
sky717[2008-02-06 12:12 AM | | | 202.158.187.160 | del | 回复回复]
沙发
使用时间:2.438毫秒
使用时间:0.750毫秒
使用时间:0.016毫秒

我测试的跟你不一样,是不是电脑的配置不同哦
发表评论
登录后再发表评论!