Analysis and summary of string operation efficiency of VBS _VBS

Source: Internet
Author: User
Tags arrays goto
Can VBS there is no similar to StringBuilder such dongdong, so our buddies can only find ways to optimize their own.
Body:
I wrote a few pieces of code to do the test and came to the following results:
' Normal string connection
StringLinkTest1 () ' performance is the worst, about 20 seconds (most of the time in 20 seconds, the entire CPU is almost 100% full load in operation)
' Normal string connection, but using a temporary variable to improve efficiency
StringLinkTest2 () ' Amazing improvement in performance, about 0.2 seconds
' Using array +join functions to handle
Stringarraytest () ' best performance, about 0.06 seconds
' There is a way of using Dictionary objects: Scripting.Dictionary, but because of the large number of consecutive use of class methods, it will directly affect the efficiency (efficiency between stringarraytest and StringLinkTest2), I'm not going to stick it here.
The result is that, in the string processing of the VBS, the efficiency problem can be solved.
The code is as follows:
Copy Code code as follows:

<%
' VBS high speed string Operation code Demo
' Huai Nan zi writing
Option Explicit
Dim Strtime,endtime
Dim Mystring,myarray,arrayindexcount,curindex
Const testnumber = 9999 ' cycle times
Strtime = Timer ()
' ============ Test begins ============
' Code Execution efficiency
' I machine configuration:
' CPU: Core dual-core 2250 CPU Frequency: 1.73G
' Memory: 1GB
' Please open the method to test each
' StringLinkTest1 () ' performance is the worst, about 20 seconds
' StringLinkTest2 () ' performance greatly improved, about 0.2 seconds
' Stringarraytest () ' performance best, about 0.06 seconds
' ============ test ends ============
' Output results
' Response.Write MyString
Endtime = Timer ()
Response.Write "Time Consuming:" & FormatNumber ((endtime-strtime) * 1000,3) & "milliseconds"
' String manipulation function, Huai nan Zi original
Sub Add (Value)
If (Curindex >= arrayindexcount) Then
Arrayindexcount = Curindex * 1.1 ' If you want to add items beyond the array subscript, expand the array capacity by 10
ReDim Preserve myarray (arrayindexcount)
End If
MyArray (curindex) = Value
Curindex = Curindex + 1
End Sub
' Test method
' using arrays for string stacking, this method has the best performance in all methods (4 times times more efficient than the StringLinkTest2 () method)
Sub Stringarraytest ()
Arrayindexcount = 20
Curindex = 0
ReDim MyArray (Arrayindexcount)
Dim I
For i = 0 to Testnumber
Add "Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Next
MyString = Join (MyArray, "")
End Sub
' Test Method 1
' Regular string concatenation
Sub StringLinkTest1 ()
Dim I,str
Dim A1
A1 = "Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
For I=0 to Testnumber
' General string Connection
Str= (STR&AMP;A1)
Next
MyString = Str
End Sub
' Test Method 2
' In regular string concatenation, the use of temporary variables to speed up, more efficient than the StringLinkTest1 () method to improve nearly 100 times times
Sub StringLinkTest2 ()
Dim i,str,a1,tmpstring
A1 = "Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
For I=0 to Testnumber
' Use temporary variable speed
Tmpstring = (tmpstring & A1)
' Cumulative every 200 times
If I mod = 0 Then
' Save temporary variable value
str = (str & tmpstring)
' Empty temporary variable value
tmpstring = ""
End If
Next
If tmpstring<> "" Then MyString = (Str & tmpstring)
End Sub
%>

If there is a mistake, please clap the bricks, hehe
you can also use arrays to stitch strings!
Copy Code code as follows:

' The simplest example is to generate NUM repeat str, such as xstring (5, "<br>") ' Output: <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


' String concatenation class public version
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.