<% 'vbs版高速字符串操作代码演示 '淮南子编写 Option explicit Dim StrTime,EndTime Dim MyString,MyArray,ArrayIndexCount,CurIndex Const TestNumber = 9999 '循环次数 StrTime = Timer() '============测试开始============ '代码执行效率 '本人机器配置: 'CPU: 酷睿双核2250 CPU频率:1.73G '内存: 1GB '请逐一开启方法进行测试 'StringLinkTest1() '性能最差,大约耗时20秒 'StringLinkTest2() '性能大大改善,大约耗时0.2秒 'StringArrayTest() '性能最佳,大约耗时0.06秒 '============测试结束============ '输出结果 'Response.Write MyString EndTime = Timer() Response.Write "耗时:" & FormatNumber((EndTime-StrTime) * 1000,3) & " 毫秒" '字符串操作函数,淮南子原创 Sub Add(Value) If (CurIndex >= ArrayIndexCount) Then ArrayIndexCount = CurIndex * 1.1 '如果欲添加项超出数组下标,则将数组容量扩增百分之10 ReDim Preserve MyArray(ArrayIndexCount) End If MyArray(CurIndex) = Value CurIndex = CurIndex + 1 End Sub '测试方法 '使用数组进行字符串叠加,在所有方法中,该方法性能最佳(效率较StringLinkTest2()的方法提升了近4倍) Sub StringArrayTest() ArrayIndexCount = 20 CurIndex = 0 ReDim MyArray(ArrayIndexCount) Dim i For i = 0 to TestNumber Add "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Next MyString = Join(MyArray,"") End Sub '测试方法1 '常规的字符串连接 Sub StringLinkTest1() Dim i,str dim a1 a1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" For i=0 to TestNumber '常规字符串连接 Str=(Str&a1) Next MyString = Str End Sub '测试方法2 '在常规的字符串连接方式中,使用临时变量来提速 ,效率较StringLinkTest1()的方法提升了近100倍 Sub StringLinkTest2() Dim i,str,a1,TmpString a1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" For i=0 to TestNumber '使用临时变量提速 TmpString = (TmpString & a1) '每二百次则进行一次累计 If i mod 200 = 0 Then '保存临时变量值 Str = (Str & TmpString) '清空临时变量值 TmpString = "" End If Next if TmpString<>"" Then MyString = (Str & TmpString) End Sub %>