Library tutorials & articles
String Concatenation Component
- Introduction
- The solution
- The results
- Conclusion
- Test Code
The results
Usage
I like to use this component for concatenation of SQL query strings, both in
VB and in ASP scripts. SQL Server devours big batches of SQL much better than
lots of little requests. Since Catter is so efficient, great gains
can be had when the size of the strings and the number of concatenations grows.
As an example, I use this component to manufacture SQL that marks all of a set
of database-modelled files and folders and their children folders and files as
public or private. If there are just a few files and folders, you can get away
with VB string concatenation. Throw in a few thousand files and deeply nested
folders, and you need the improved performance of Catter.
Performance
Catter outperforms standard VB string concatenation after several dozen concatenations.
Here's The results can be seen below. If you're interested in the code tests
I used, check out the VB test code used. Note that
is employs the use of a free component, ASP
Profiler. To run this code you will need to download and install the component.
|
Concatenations
|
Length of String
|
VBScript Concatenation
|
StrCat.Catter Concatenation
|
| 1 | 44 | 0.01 | 0.05 |
| 5 | 44 | 0.02 | 0.06 |
| 10 | 44 | 0.02 | 0.08 |
| 25 | 44 | 0.05 | 0.31 |
| 50 | 44 | 0.14 | 0.35 |
| 100 | 44 | 1.07 | 0.44 |
| 200 | 44 | 1.86 | 0.81 |
| 500 | 44 | 12.19 | 2.30 |
| 1000 | 44 | 140.20 | 5.51 |
| 2000 | 44 | 940.53 | 12.41 |
|
|
|||
| 200 | 484 | 150.13 | 11.31 |
| 500 | 484 | 1,467.19 | 25.16 |
| 1000 | 484 | 6,414.08 | 58.50 |
| 2000 | 484 | 26,419.20 | 112.30 |
You can see that it takes about three times longer to get Catter
up to speed for a single concatenation, and even after a few dozen Catter
lags behind. But once VB starts to slow down, it does so disastrously - quadratically,
in fact - and Catter keeps its cool, staying almost linear in terms
of time versus concatenations. I'd hate to be the person waiting 25 seconds for
VB to concatenate some strings!
Implementation
Catter is an STL vector of STL wstrings. Catter's implementation
calls on the reserve methods of both the vector and wstring classes. The implementations
of these classes are designed so that their execution time stays linear under
these circumstances. They accomplish this by doubling their allocated memory
when they need to grow, thereby avoiding making too many allocations and fragmenting
the heap.
Related articles
Related discussion
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Making the conversion from Visual Basic to .NET
by JimiJ (1 replies)
-
VS2005 app's won't run on another machine
by ted4444 (0 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
A great idea! You can also reduce concatenation times by using arrays and the built-in Join() function. No components required. Similar performance gain. Very readable syntax (once you understand the Join arguments).
StringVar = JOIN( StringArray(), ElementSeparatorText )
Redim S(4)
S(0) = "This is text line 1"
S(1) = "This is text line 2"
S(2) = "This is text line 3"
S(3) = "This is text line 4"
S(4) = "This is text line 5"
'assign lines to string variable with comma separator
vMyString = join(S, ",")
'dump lines to screen, one element per visual line ("&" executes once)
response.write join(S, "<br>" & VbCrLf)
'output array as a table row (elements are separated with "</td><td>")
response.write "<tr><td>" & join(S, "</td><td>" ) & "</td></tr>"
'build an option list of codes that exist in a database
set rs = CreateObject("ADOR.Recordset")
rs.Open "select [TypeCode] from [CodeTable] order by [TypeCode]", myConnectString, adOpenStatic
S = rs.getrows '<-- very fast!
rs.close
response.write = "<select ...>" & vbcrlf
response.write = "<option>" & join(S, "</option>" & vbcrlf & "<option>" ) & "</option>" & vbcrlf
response.write = "</select>" & vbcrlf
'clear array from memory (if you want to free up memory right away)
erase S
This thread is for discussions of String Concatenation Component.