开发者

Is HttpResponse.Write different from StringWriter.Write?

开发者 https://www.devze.com 2022-12-19 10:35 出处:网络
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开发者_Go百科 HttpResponse.Write several times could it pote

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开发者_Go百科 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消