On Tue, 16 May 2006 13:00:01 -0500, Giles <gi***@nospam.com> wrote:
When assembling an HTML string from a database before sending it to the
client (ie keeping the connection as short as possible), adding to an
existing string (strOut=strOut & strNextLine) takes time and can defeat
the
object.
Is putting each line into an array, and re-dimming the array by +1 each
time
quicker? Is there a better way?
I ran some profiling tests of all the ways I could think of offhand to
assemble a string. Using the concatenation operator (&) is by far the
slowest. In my tests this method took over a hundred times as long as
using the Join() function on a statically-dimensioned array.
Here are the results I got:
Method Time
----------------------------------------- ------------
Concatenation operator 51.1457s
Write to ADODB.Stream object 2.582813s
Join Scripting.Dictionary Items() array 0.2144531s
Join dynamically-dimensioned array 0.0609375s
Join statically-dimensioned array 0.046875s
Note that I doubled the size of the dynamically-dimensioned array each
time it filled. Increasing the array's size by one element at a time would
perform considerably worse.
I am surprised by how poorly the Stream object fared. I don't use it very
often, so perhaps I am using it sub-optimally. I have included the script
I used in my tests below.
Option Explicit
Function Prof(f)
Dim start, finish, i, total
total = 0
For i = 0 To 9
start = Timer()
f
finish = Timer()
total = total + finish - start
Next
Prof = total/10
End Function
Sub ProfConcat()
Dim i, s
s = String(100, "s")
For i = 1 To 10000
s = s & String(100, "s")
Next
End Sub
WScript.Echo "Concat" & vbTab & Prof(GetRef("ProfConcat")) & "s"
Sub ProfDict()
Dim i, s, d
Set d = CreateObject("Scripting.Dictionary")
For i = 1 To 10000
d.Add d.Count, String(100, "s")
Next
s = Join(d.Items(), "")
End Sub
WScript.Echo "Dict" & vbTab & Prof(GetRef("ProfDict")) & "s"
Sub ProfDim()
Dim i, s, a(10000)
For i = 1 To 10000
a(i) = String(100, "s")
Next
s = Join(a, "")
End Sub
WScript.Echo "Dim" & vbTab & Prof(GetRef("ProfDim")) & "s"
Sub ProfReDim()
Dim i, s, a()
ReDim a(1)
For i = 1 To 10000
If UBound(a) < i Then ReDim Preserve a(i*2)
a(i) = String(100, "s")
Next
s = Join(a, "")
End Sub
WScript.Echo "ReDim" & vbTab & Prof(GetRef("ProfReDim")) & "s"
Sub ProfStream()
Dim i, s, o
Set o = CreateObject("ADODB.Stream")
o.Open
For i = 1 To 10000
o.WriteText String(100, "s")
Next
o.Position = 0
s = o.ReadText()
End Sub
WScript.Echo "Stream" & vbTab & Prof(GetRef("ProfStream")) & "s"
--
Justin Piper
Bizco Technologies
http://www.bizco.com/