资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vbs的字符串操作的效率分析-创新互联

这篇文章主要介绍“vbs的字符串操作的效率分析”,在日常操作中,相信很多人在vbs的字符串操作的效率分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vbs的字符串操作的效率分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

成都创新互联公司专注于都安企业网站建设,成都响应式网站建设公司,商城系统网站开发。都安网站建设公司,为都安等地区提供建站服务。全流程按需设计网站,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务


正文:
我写了几段代码做了测试,得出以下结果:
'普通字符串连接
StringLinkTest1() '性能最差,大约耗时20秒(最要命的是在这20秒内,整个CPU几乎是100%满负荷在运行)
'普通字符串连接,但使用了临时变量来提升效率
StringLinkTest2() '性能令人吃惊的改善,大约耗时0.2秒
'使用数组+Join函数处理
StringArrayTest() '性能很好,大约耗时0.06秒
'本来还有个方法,是利用字典对象:Scripting.Dictionary 来操作的,但由于在大量连续使用的类方法的情况下,会直接影响效率(效率介于StringArrayTest和StringLinkTest2之间),在此就不贴了
得出的结果就是,在vbs的字符串处理上,还是可以解决效率问题的。
代码如下:


复制代码 代码如下:


<%
'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
%>




也可以用数组来拼接字符串啦!


复制代码 代码如下:


'最简单的例子,生成num个重复的str,例如 XString(5,"
") '输出:





Function XString(num,str)
On Error Resume Next
Dim i,a
Redim a(num-1)
For i=0 To num-1
a(i)=str
Next
XString=Join(a,"")
On Error GoTo 0
End Function


'字符串拼接类公共版
Class clsStrCat
Private aFStrings()
Private iFSPos,iFSLen,iFSIncr
Private Sub Class_Initialize()
On Error Resume Next
iFSIncr = STRCATBUF
If Err Then iFSIncr = 200 : Err.Clear
Reset
On Error GoTo 0
End Sub
Private Sub Class_Terminate()
Erase aFStrings
End Sub
Public Property Let Item(ByRef sData)
If iFSPos > iFSLen Then
iFSLen = iFSPos + iFSIncr
ReDim Preserve aFStrings(iFSLen)
End If
aFStrings(iFSPos) = sData
iFSPos = iFSPos + 1
End Property
Public Default Property Get Item()
Item = Join(aFStrings, "")
End Property
Public Sub Reset()
iFSPos = 0
iFSLen = iFSIncr
ReDim aFStrings(iFSLen)
End Sub
Public Sub Resize(n)
If Not IsNumeric(n) Then Exit Sub
iFSPos = 0
iFSIncr = n
iFSLen = iFSIncr
ReDim aFStrings(iFSLen)
End Sub
Public Property Get Strs()
Strs=aFStrings
End Property
Public Property Get Count()
Count=iFSPos
End Property
Public Property Get IsInit()
If iFSPos=0 Then IsInit=True Else IsInit=False
End Property
End Class


到此,关于“vbs的字符串操作的效率分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


文章题目:vbs的字符串操作的效率分析-创新互联
网站链接:http://cdkjz.cn/article/jsghd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220