473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String Building

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?
Thanks
Giles
May 16 '06 #1
16 1867
Giles 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?


Depends on the size of the string. For large strings, putting the fragments
into an array and using Join to create the string will usually be much
quicker than concatenation.
However, you are much better off initially dimming the array to the size it
needs to be rather than redimming it every time a string fragment is added,
even if you initially dim it to a size that is larger than what you need.
Repeating the ReDim operation for every string fragment will undo all the
gains you achieved when you eliminated the concatenation.

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
May 16 '06 #2
Giles 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?
Thanks
Giles

And if you're really interested in minimizing connection time, look into
using GetRows and GetString:
http://www.aspfaq.com/show.asp?id=2467
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
May 16 '06 #3
Giles wrote:
Is putting each line into an array, and re-dimming the array
by +1 each time quicker? Is there a better way?


Yes. Array.push() is especially convenient for avoiding this kind of
nonsense.

But I suppose you mean in VBScript. I suspect, as Bob did, that one of the
recordset methods will do what you need.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
May 16 '06 #4

"Giles" <gi***@nospam.com> wrote in message
news:Ox*************@TK2MSFTNGP02.phx.gbl...
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?
Thanks
Giles

I guess at the end of all that you would do:-

Response.Write strOut

So why not replace:-

strOut = strOut & strNextLine

with:-

Response.Write strNextLine

Using GetRows as Bob suggests is what will minimise connection time.
(Not that I'm a big fan of GetRows myself <bg> )

Anthony.

May 16 '06 #5
> "Giles" <gi***@nospam.com> wrote in message
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?
Thanks
Giles

Anthony wrote:
So why not replace:-
strOut = strOut & strNextLine
with:-
Response.Write strNextLine


Doesn't response.write wait for the client to "acknowledge receipt" ? so a
slow internet connection could keep the db connection open for a long time -
like really ages with some modems / isp's? My server-fuhrer threatened me
with severe whipping for doing that, which is why I changed to making a
string first, closing the conn, then dumping the string. However, now I am
tying up the processor (can't win!).
Bob's suggestion about dimming first (perhaps with a COUNT to ensure the
right size) and GetRows/GetString seems like a top banana to me
May 16 '06 #6
Giles wrote:
Doesn't response.write wait for the client to "acknowledge
receipt" ? so a slow internet connection could keep the db
connection open for a long time - like really ages with
some modems / isp's?
No.

Response.Write actually puts the text into the Response Buffer, which is
flushed (sent to the client) once the entire document is assembled.
Connection speed is irrelevant.

You might be able to imapct performance by turning off buffering or using
explicit Response.Flush calls, but those are atypical things to do.
Bob's suggestion about dimming first (perhaps with a COUNT
to ensure the right size) and GetRows/GetString seems like
a top banana to me


If the recordset is big enough that you are noticing this, use GetRows or
GetString. They are orders of magnitude faster than VBScript arrays and/or
string concatenation.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
May 17 '06 #7

"Giles" <gi***@nospam.com> wrote in message
news:eX**************@TK2MSFTNGP05.phx.gbl...
"Giles" <gi***@nospam.com> wrote in message
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?
Thanks
Giles

Anthony wrote:
So why not replace:-
strOut = strOut & strNextLine
with:-
Response.Write strNextLine


Doesn't response.write wait for the client to "acknowledge receipt" ? so a
slow internet connection could keep the db connection open for a long

time - like really ages with some modems / isp's? My server-fuhrer threatened me
with severe whipping for doing that, which is why I changed to making a
string first, closing the conn, then dumping the string. However, now I am
tying up the processor (can't win!).

Like Dave said with buffering turned on then Response.Write doesn't actually
involve actually sending anything. So using Response.Write will avoid the
inefficient allocation/dealloaction of memory that string concatenation
incurrs.
Bob's suggestion about dimming first (perhaps with a COUNT to ensure the
right size) and GetRows/GetString seems like a top banana to me


An initial Redim of the array is unnecessary GetRows returns an array of the
correct dimensions. Hence if performance is paramount then GetRows coupled
with techniques that avoid excessive string concatenation (such as the Join
function or Response.Write) are what you will be needing.

Anthony.

May 17 '06 #8
Anthony Jones wrote:

An initial Redim of the array is unnecessary GetRows returns an array
of the correct dimensions. Hence if performance is paramount then
GetRows coupled with techniques that avoid excessive string
concatenation (such as the Join function or Response.Write) are what
you will be needing.

Oops, you lost sight of the original problem, which was creating a large
html string. GetRows will quickly get the data into an array, upon which he
can close the connection and process the data in the array. I don't believe
that Join can be used on a multidimensional array ...
If he is now going to build a large string using the data in the getrows
array, then yes, an array initially dimmed to the proper size may still be
the best solution.

However, I am going along with your (and Dave's) earlier suggestion to
simply response.write the string fragments built from looping through the
getrows array, rather than concatenating them before writing them. I was not
sure about the real reason for creating the large string when I wrote my
initial response.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
May 17 '06 #9
"Bob Barrows [MVP]" wrote <snip>
However, I am going along with your (and Dave's) earlier suggestion to
simply response.write the string fragments built from looping through the
getrows array, rather than concatenating them before writing them. I was
not sure about the real reason for creating the large string when I wrote
my initial response.


Thanks everyone, insights much appreciated. I guess I had been misinformed
about response.write, and had been making life unnecessarily difficult for
myself.
Giles
May 17 '06 #10
Giles wrote:
Thanks everyone, insights much appreciated. I guess I had
been misinformed about response.write, and had been making
life unnecessarily difficult for myself.


If you are still concerned about keeping connections open, keep in mind that
you can do any of these things (Response.Write, rs.GetRows, rs.GetString,
even the dreaded concatenation) with disconnected recordsets. That will ease
the concerns of your "server-fuhrer".

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
May 17 '06 #11
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/
May 17 '06 #12
On Wed, 17 May 2006 12:28:38 -0500, Justin Piper <jp****@bizco.com> wrote:
In my tests this method took over a hundred times as long as using the
Join() function on a statically-dimensioned array.


Whoops, make that over a /thousand/ times as long.
--
Justin Piper
Bizco Technologies
http://www.bizco.com/
May 17 '06 #13
>
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.


That'll be due to the encoding the stream object will be performing on the
string. It doesn't just dump the unicode string given into the storage but
encodes it to the currently selected codepage.

Anthony.
May 17 '06 #14
On Wed, 17 May 2006 15:59:03 -0500, Anthony Jones <An*@yadayadayada.com>
wrote:
I am surprised by how poorly the Stream object fared.


That'll be due to the encoding the stream object will be performing on
the
string. It doesn't just dump the unicode string given into the storage
but
encodes it to the currently selected codepage.


Ah, that makes sense. In that case, I suspect it doesn't check whether the
text needs to be re-encoded before doing so, as explicitly setting the
locale to "en-us" and the Stream object's codepage to "windows-1252"
didn't have any effect on performance.

--
Justin Piper
Bizco Technologies
http://www.bizco.com/
May 17 '06 #15

"Justin Piper" <jp****@bizco.com> wrote in message
news:op***************@luxembourg.psg.bizcotech.co m...
On Wed, 17 May 2006 15:59:03 -0500, Anthony Jones <An*@yadayadayada.com>
wrote:
I am surprised by how poorly the Stream object fared.
That'll be due to the encoding the stream object will be performing on
the
string. It doesn't just dump the unicode string given into the storage
but
encodes it to the currently selected codepage.


Ah, that makes sense. In that case, I suspect it doesn't check whether the
text needs to be re-encoded before doing so, as explicitly setting the
locale to "en-us" and the Stream object's codepage to "windows-1252"
didn't have any effect on performance.


All strings in ASP are unicode. Setting the Stream object's encoding to
"UTF-16" might make a difference since it shouldn't have to encode the
string.
--
Justin Piper
Bizco Technologies
http://www.bizco.com/

May 18 '06 #16
On Thu, 18 May 2006 09:59:06 -0500, Anthony Jones <An*@yadayadayada.com>
wrote:
All strings in ASP are unicode. Setting the Stream object's encoding to
"UTF-16" might make a difference since it shouldn't have to encode the
string.


No such luck. The docs say that "unicode" is the default value of
..Charset, and explicitly setting it to "utf-16" or "utf-8" unfortunately
doesn't yield any performance gains.

--
Justin Piper
Bizco Technologies
http://www.bizco.com/
May 18 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
3
by: Dave Byron | last post by:
I am having trouble with DBNull's from my SQL server. I am building/converting a asset web app from Access and my db has nulls on various fields . I have tried searching newsgroups and tried to get...
4
by: John | last post by:
Hi What are the advantages of a stringbuilder compared to a string? Thanks Regards
5
by: feng | last post by:
OK, all I want is writting a string that represents an XML document into a file, say C:\test.xml. This kind of task used to be so easy with vb6. But now, with VB.Net, it seems turned into a big...
12
by: DumberThanSnot | last post by:
is there a faster way to copy an ArrayList of strings to a string other than a tight loop. In it's most simple terms, I'm currently using something like this... ---------------------------- ...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
8
by: Jacob Arthur | last post by:
How would I go about using a custom select string that is passed from a form to the SelectCommand parameter of SqlDataSource? I tried: SelectCommand = "<% Request.Form("hdnSelect") %>" but I...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
13
by: Tony Johansson | last post by:
Hello! I read in a book and here is a question and the answer that I'm not satisfied with. When should you use the StringBuilder class instead of the String class. 1.When building a string from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.