views:

192

answers:

2

I found myself writing to stringwriter then at the end of the function do a resp.Write(sw.ToString()). Is this unnecessary? If i were to use HttpResponse.Write several times could it potentially send data out multiple times (chunks) even if my page is 2k? How is HttpResponse.Write different from StringWriter.Write?

+1  A: 

Use Response.Write for Formatting Output

Where possible, avoid using loops to concatenate strings for formatting page layout. Consider using Response.Write instead. This approach writes output to the ASP.NET response buffers. When you are looping through datasets or XML documents, using Response.Write is a highly efficient approach. It is more efficient than concatenating the content by using the += operator before writing the content back to the client. Response.Write internally appends strings to a reusable buffer so that it does not suffer the performance overhead of allocating memory, in addition to cleaning that memory up.

From

Improving ASP.NET Performance

rahul
Please don't just copy other people's work (http://msdn.microsoft.com/en-us/library/ms998549.aspx) without attribution.
Matthew Flaschen
I was about to link the document, but couldn't get the link right. This one was from a PDF file.
rahul
+1  A: 

There's no need to create an intermediate string in this case. You don't need to worry about what the network stack is doing (whether HttpResponse.Write is broken into multiple low-level socket writes). However, you will get the best performance simply by writing the data as soon as you have it.

Matthew Flaschen