Response.Write. This ASP.NET method adds string data to the Response buffer. With it, no intermediate conversions are needed. Developers often write text with Response.Write.
Some improvements. We can improve Response.Write calls to be faster. Use Response.Write on each individual string or character. If possible, avoid all concatenations.
First example. Here we use Response.Write, but append strings to pass as parameters. This is slower than necessary because the strings require copying and allocation.
//// 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.
//// 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.
//// 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.
Note Technically this version changes the behavior. For the requirements of the program, though, it did not matter.
//// D. Fastest version (1318 ms)//
foreach (var pair in _diskDictionary)
{
Response.Write(pair.Key);
Response.Write(':');
Response.Write(pair.Value.ToString());
Response.Write('\n');
}
Improvement 2. Here is 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.
Tip We can cache the Response object locally, and performance improves by nearly 10% in my benchmark.
And The numbers I measured here were 748 before and 671 after, on a smaller data set.
//// 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');
}
A discussion. 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.
Finally Let's look at the Improving ASP.NET Performance document from Microsoft. It tells us what we need to know.
Detail The book advises us to avoid strings—we do not want to use simple strings to build up output.
Summary. We used the Response.Write method for appending strings to the Response. We can call Response.Write with individual string arguments for the clearest and fastest code.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 27, 2022 (simplify).