当前位置: 移动技术网 > IT编程>脚本编程>VBScript > vbs的字符串操作效率分析总结

vbs的字符串操作效率分析总结

2017年12月08日  | 移动技术网IT编程  | 我要评论

湛蓝徽章txt全集下载,当归的功效与作用及禁忌,梧州网通露天影院

可vbs根本就没有类似于stringbuilder这样的东东,所以咱哥们只能自己想办法优化了。
正文:
我写了几段代码做了测试,得出以下结果:
'普通字符串连接
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,"<br>") '输出: <br><br><br><br><br>
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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网