Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best use of storing strings (streams?)

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
When I first started in web page develompent I was using Java Servlets. One thing I remember coming up again and again was that rather than using strings to store your HTML, before dumping to the response object, was that you should use a stream.

Does this hold true for ASP?

I know that using strings to hold large amounts of text can be detrimental to performance. The building and tearing down of strings can cause huge performance hits on the page. Rather than always writing to the response stream (which I don't always want to do), I was wondering if it would be better to create a temporary stream, write to that and then dump it to the response stream when I was ready.

If this is a good way to go about it:
[ul][li]What type of stream should be used?[/li]
[li]Are there ways to tie the stream to the response object (ie.
Code:
stream.flush
sends to the response object)[/li]
[li]What are the advantages/disadvantages of doing something this way?[/li][/ul]
________________________________________
Nature and Nature's laws lay hid in night
God said "Let Newton be", and all was light
~Alexander Pope~
 
A similar - recent thread: thread333-422486


How I handle the problem.
I had a similar problem. I was concatenting an HTML string to output. The length of the string was over 1 million characters. Addin 50 characters to a string that is already over 1 million long takes a very long time - you drag the server down exponentially as you add to the string. The solution: create a class to handle the string separately (parallel process = very fast) I got a page that took over 15 minutes to display down to about 3 seconds....
<%
Class strCat
Private IntCntr
Private strArray()
Private intLength
Public Property Get Length
Length = intLength
End Property
Public Property Let Length( ByVal intLen)
intLength = intLen
IntCntr = 0
Redim strArray(1)
strArray(0) = &quot;&quot;
Redim strArray(intLength)
End Property
Public Property Get Value
Value = Join(strArray,&quot;&quot;)
End Property
Private Sub Class_Initialize()
IntCntr = 0
Length = 100000
End Sub
Public function Add( strToAdd)
strArray(IntCntr) = strToAdd
IntCntr = IntCntr + 1
End function
End Class

dim strOut
set strOut = new strCat
My stored procedure returns HTML code
do while not objrs.eof
if not isNull(objrs(&quot;detail&quot;)) then strOut.add(objrs(&quot;detail&quot;))
if not isNull(objrs(&quot;value&quot;)) then strOut.add(objrs(&quot;value&quot;)&vbcrlf) else strOut.add(vbcrlf)
objrs.movenext
loop

%>

<%=strOut.value%>
-----------------------------------------------------------------
DIM bulb
SET bulb = NEW brain

mikewolf@tst-us.com
 
That is similar to how I have solved that problem in the past and how I handle most of the stuff on my pages now (smaller strings built and then appended to larger strings, which in turn get appended to larger strings).

I am more curious about streams in particular. I know that in other languages, they are signifigantly faster than strings. This occurs because of the way they manage memory (I'm not sure on the particulars). Because a stream assumes you are always appending data (never inserting somewhere in the middle) they require less processing to build. Based on this a single stream of 1 million characters, built 50 at a time, would not slow down over time. It would take just as long to append the 1st 50 as it would the 10,000th 50. ________________________________________
Nature and Nature's laws lay hid in night
God said &quot;Let Newton be&quot;, and all was light
~Alexander Pope~
 
If you look at the class, it creates an array of individual strings and then uses one join function only when you call them for output. It is very fast. And it will not &quot;slow down over time&quot;.

I haven't heard the term streams used in asp - that doesn't mean they don't exist - I just haven't heard of them.... -----------------------------------------------------------------
DIM bulb
SET bulb = NEW brain

mikewolf@tst-us.com
 
Your class does solve the issue of HUGE strings nicely.

I am more or less just curious about the use of streams in VBA. I notice they don't get used a lot in ASP and yet are a basic premise in other languages (for example C and Java).

It appears to me that streams have all of the same benefits in VBA as they do in other languages, and yet they are rarely used. Is this a tool that has been overlooked, and if so was it overlooked for a reason or just because strings are very powerful in VBA?

As for never hearing of them...
There are only three streams in VBA (that I know of):
[ol][li]ADODB.Stream:
[ul][li]This one can be declared directly:
Code:
set stm = server.createobject(&quot;ADODB.Stream&quot;)
[/li]
[li]Can be used to load data directly from file, from a database, or written to by the program.[/li]
[li]Handles both binary and text data.[/li][/ul][/li]
[li]Scripting.TextStream:
[ul][li]Can only be declared from the Scripting.FileSystemObject:
Code:
set stm = fso.OpenAsTextStream(&quot;somepath&quot;)
[/li]
[li]It is, esentially, a reference to a text file. It can only be used to read or write from a text file.[li]Handles only text data.[/li][/ul][/li]
[li]Response Object:
[ul][li]The response object itself is a stream. It cannot be declared.[/li][li]Handles both binary and text data.[/li][/ul][/li]
[/ol]

One defining feature of streams is the ability to flush them. A stream ties into an underlying object and, upon command, will send its entire contents to that object.

In the case of the response object, it is tied to the output buffer for the server. Once it is has data in it you can then flush it, thereby dumping all of its contents to the client.
________________________________________
Ignorance is a beautiful thing:
You don't know what you can't do...
 
In the other thread that I mentioned, they were using the &quot;RESPONSE.FLUSH&quot; command. If the response object is itself a stream, then I guess that's why I never hear of the others. In asp most everything is output using the response object.... -----------------------------------------------------------------
DIM bulb
SET bulb = NEW brain

mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top