article

speeding up concatenating

Email
Submitted on: 1/5/2015 10:30:00 PM
By: Robbert Nix (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 2264
 
     Concatenating is not very efficient in VBS, especially when a large number of small string are concatenated. Here is how to speed it up

 
				






Speeding up String Concatenation
<--[if gte mso 9]>
 
 R. Nix
 R. Nix
 2
 4
 2002-01-08T13:10:00Z
 2002-01-08T13:10:00Z
 3
 504
 2875
 23
 5
 3530
 9.2720
 
 
 
 
<--[if gte mso 9]>
 
 DocumentEmail
 0 pt
 2
 2
 




Speeding up String Concatenation

 

When you dynamically want to generate an output page using information from a table, you could use the following function to perform this operation.

 

Method 1

Function TableContent(…)

      Dim strContent

 

     

      Do While Not recordset.EOF

            StrContent = strContent & recordset.Fields([cell])

           

      Loop

 

      TableContent = strContent

End Function

 

Method 1 is not an efficient if the number of concatenations are large, since the concatenating operation <strContent = strContent & recordset.Fields([cell])> copies strContent and recordset.Fields([cell]) to a temporary space in addition to a copy of the result. The total number of copied characters is in the order of the result-string length times the number of concatenations.

 

In VB this problem can be avoided by using the mid$ function to replace a part of a long empty string <mid$(strContent,…)=[newstr]>, but in VBS this mid function cannot be used this way.

 

In VBS the Method 2 can be used with the same end result.  In this code part(index) will be at least 30*2^i bytes long. (Since strContent has 30 elements, the function will work until the maximum length of a string is reached. Currently this is over 2GB)

It can be easily verified that every character is part of a concatenation operation only max(index) times.

 

Therefore, the total number of copied characters due to concatenations will be in the order of the length of the result string times the logarithm of the result sting length (strict mathematical proof is difficult, since the length of the new-added strings may vary). The total number of concatenations will be the same this way, but since short string concatenations occur frequently, while long string concatenations occur less often, the total length of all concatenated strings decreases.

 


Method 2

Function TableContent(…)

      Dim strContent(30)

 

     

sub to clear the content string

      ClearString strContent 

 

      Do While Not recordset.EOF

 

            ‘add to the result string

            AddString strContent, recordset.Fields([cell])     

           

      Loop

 

      TableContent = fnReadString(strContent)

 

End Function

 

The following subs and functions need to be included

 

Sub ClearString(part)

 

      Dim index ‘as integer

 

      For index = 0 to 30

            part(index) = “”

      Next

End Sub

 

Sub AddString(part, newString)

 

      Dim tmp   ‘as string

      Dim index ‘as integer

 

      part(0) = part(0) & newString

 

      If Len(part(0)) > 30 Then

 

            index = 0

            tmp = “”

 

            Do

                  tmp = part(index) & tmp

                  part(index) = “”

                  index = index + 1

 

            Loop until part(index) = “”

 

            part(index) = tmp

      End If

 

End Sub

 


Function fnReadString(part)

 

      Dim tmp   ‘as string

      Dim index ‘as integer

 

      tmp = “”

 

      For index = 0 to 30

 

            If part(index) <> “” Then

 

                  tmp = part(index) & tmp

            End If

      Next

 

      FnReadString = tmp

 

End Function

 

Comparing both methods

Consider concatenating the alphabet one character at the time. Then the following concatenations occur.

For simplicity sake the maximum length of part(0) in the AddString subroutine is set to 1 (If Len(part(0) > 1 Then)

 

Step

Concatenations method 1

Concatenations method 2

1

 

 

2

a&b

a&b

3

ab&c

 

4

abc&d

c&d, ab&cd

5

abcd&e

 

6

abcde&f

e&f

7

abcdef&g

 

8

abcdefg&h

g&h, ef&gh, abcd&efgh

9

abcdefgh&I

 

10

abcdefghi&j

I&j

 

etc.

 

 

I have run tests, series of 10,000 string concentrations with a length of 100 * (rnd() ^ 3). Using method 1, it took 30 seconds to process the script, while method 2 took less than a second. I used a Pentium 3, 500 Mhz Processor with 256Mb RAM machine.

 

 


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.