ASP.NET Response.Write

ASPNET web programming framework

Response.Write adds string data to the Response buffer. With it, no intermediate conversions are needed. Developers often write text with Response.Write. We improve Response.Write calls to be faster.

Tip:Use Response.Write on each individual string or character. If possible, avoid all concatenations.

Example

One: 1

First, here we will assume you aren't using string appends and are already using Response.Write. The following code is the version I improved. It walks through Dictionary keys and is considered the slow version.

Version A: C#

//
// A. Slow version (1568 ms)
//
foreach (var pair in _diskDictionary)
{
    Response.Write(pair.Key + ":" + pair.Value.ToString() + Environment.NewLine);
}

Even slower version. I tried eliminating the repeated Write calls and simply using a temporary StringBuilder. I varied the capacity of the StringBuilder, but this next code worked well.

StringBuilder
Version B: C#

//
// B. Slower version (1614 ms)
//
StringBuilder builder = new StringBuilder();
foreach (var pair in _diskDictionary)
{
    builder.Append(pair.Key).Append(":").Append(pair.Value.ToString()).AppendLine();
}
Response.Write(builder.ToString());

Faster version. Here I made a couple changes. I split up the Response.Write calls to send one argument at a time and not concatenate anything beforehand. This was a substantial speedup.

Version C: C#

//
// C. Faster version (1474 ms)
//
foreach (var pair in _diskDictionary)
{
    Response.Write(pair.Key);
    Response.Write(':');
    Response.Write(pair.Value.ToString());
    Response.Write(Environment.NewLine);
}

Improvement

Next we see a further improvement. I changed the newline to be a simple '\n' character. Environment.NewLine is "\r\n" which is twice as long. This changes the output but in this case it didn't matter.

Version D: C#

//
// D. Fastest version (1318 ms)
//
foreach (var pair in _diskDictionary)
{
    Response.Write(pair.Key);
    Response.Write(':');
    Response.Write(pair.Value.ToString());
    Response.Write('\n');
}

Benchmark

Performance optimization

I ran the above code fragments 20,000 times each with a dictionary of 180 key-value pairs. The output file was 4 KB and I used Response.Clear() to erase the buffer each time. The figures from the experiment are available here.

Response.Write method in ASP.NET performance
    20000 iterations.

Method A: 1568 ms
	  Concats string parameter of Response.Write.

Method B: 1614 ms
	  Appends data to StringBuilder than writes that.

Method C: 1474 ms
	  Writes each part of data individually to Response.

Method D: 1318 ms
	  Same as Method C except...
	  Uses UNIX newline instead of Windows newline.

Method E: 1200 ms
	  Stores HttpResponse as local variable.
	  Only accesses Response property once.
	  Is an estimate; was taken on different scale.

Improvement 2

Percentage symbol

After writing this article I discovered another improvement. I looked carefully at the Response type in IL Disassembler, and it is accessed through a property. Properties are slower than local instances.

We can cache the Response object locally, and performance improves by nearly 10% in my benchmark. The numbers I measured here were 748 before and 671 after, on a smaller data set.

Version E: C#

//
// E. Faster than fastest version
//
HttpResponse r = Response;
foreach (var pair in _diskDictionary)
{
    r.Write(pair.Key);
    r.Write(':');
    r.Write(pair.Value.ToString());
    r.Write('\n');
}

Discussion

Squares

ASP.NET works with the Response buffer. The best approach is to pass in each separate string.
This is faster,
uses less memory,
and even simpler to read. I noted a performance degradation when not calling ToString on the ints.

Note:I have not carefully investigated this.
Response.Write may have slower code for ints.

More resources. Alik Levin has an interesting post with how Response.Write is faster than alternative methods. This aligns with the Microsoft guidelines linked to above. Response.Write is the best choice here.

Best ASP.NET Performance Winner: MSDNFile: text page

Finally, let's look at the Improving ASP.NET Performance document from Microsoft. It tells us what we need to know, but here I provide benchmarks and examples. What do Microsoft's guidelines tell us? Avoid strings.

Tip:This is pretty obvious, but you don't want to use simple strings to build up output. This would involve the + operator.

Response.Write internally appends strings to a reusable buffer so that it does not suffer the performance overhead of allocating memory....

Improving ASP.NET Performance: MSDN

Summary

The C# programming language

We looked at usages of the Response.Write method for appending strings to the Response buffer in the ASP.NET Framework. You can call Response.Write with individual string arguments for the clearest and fastest code.

Review:There's a good and a bad way to use Response.Write. Avoiding string concat operations is the good way.


C#: ASP.NET