ASP高效率字符串连接类
<%
'fast string class
class strCat
dim index, ub, ar()
Private Sub Class_Initialize()
'statements
redim ar(50)
index=0
ub=10
End Sub
Private Sub Class_Terminate()
'statements
erase ar
End Sub
public default Sub Add(value)
ar(index)=value
index=index+1
if index>ub then
ub=ub+50
redim preserve ar(ub)
end if
end sub
public Function dump
redim preserve ar(index-1)
dump=join(ar,"")
end function
end class
'first traditional method
t1=time
os=""
for i=1 to 5000
os=os & "This is the slow method of building strings"
next
t2=time
t3=time
set cat= new strCat
for i=1 to 5000
cat("This is the slow method of building strings")
next
s= cat.dump
set cat = nothing
t4=time
response.Write("Using the & operator:" & cstr(t1) & " - " & cstr(t2) & "<br>Using the fast string class: " & cstr(t3) & " - cstr(t4) & "<br>")
%>
'fast string class
class strCat
dim index, ub, ar()
Private Sub Class_Initialize()
'statements
redim ar(50)
index=0
ub=10
End Sub
Private Sub Class_Terminate()
'statements
erase ar
End Sub
public default Sub Add(value)
ar(index)=value
index=index+1
if index>ub then
ub=ub+50
redim preserve ar(ub)
end if
end sub
public Function dump
redim preserve ar(index-1)
dump=join(ar,"")
end function
end class
'first traditional method
t1=time
os=""
for i=1 to 5000
os=os & "This is the slow method of building strings"
next
t2=time
t3=time
set cat= new strCat
for i=1 to 5000
cat("This is the slow method of building strings")
next
s= cat.dump
set cat = nothing
t4=time
response.Write("Using the & operator:" & cstr(t1) & " - " & cstr(t2) & "<br>Using the fast string class: " & cstr(t3) & " - cstr(t4) & "<br>")
%>