Foreach is slower than building a string on .cs?

Today, a colleague told me that he builds the entire line faster on ascx.cs trought foreach and String Builder (so write the html code in the line) and place it at the end on ascx as <%=myStringGenerateByStringBuilder%>, rather than using foreach directly on ascx, for example:

<% foreach (var thing in things)
   { %>
   <div><p><%= thing.Description %></p></div>
<% }; %>

This is because open <%and close are %>very expensive.

It's true? I'm not talking about code management (the second question is what I'm using is very clean and simple), I'm talking about performance when generating code.

+3
source share
5 answers

Well, some tests:

void Main()
{
    var things = Enumerable
            .Range(1, 2000000)
            .Select(r => new Thing {Description = r.ToString()})
            .ToList();

    var times = Enumerable.Range(1, 10)
        .Select(t => new
            {   
                Method1 = Test(Method1, things),
                Method2 = Test(Method2, things),
            }
    ).ToList();
    var average = new {
            Method1 = TimeSpan.FromMilliseconds( times.Sum(t=>t.Method1.TotalMilliseconds) / times.Count),
            Method2 = TimeSpan.FromMilliseconds( times.Sum(t=>t.Method2.TotalMilliseconds) / times.Count),
    };
    average.Dump();
}

public TimeSpan Test(Action<IEnumerable<Thing>> action, IEnumerable<Thing> things)
{
    var stopWatch = new Stopwatch();
    stopWatch.Start();
    action(things);
    stopWatch.Stop();
    return stopWatch.Elapsed;
}

public void Method1(IEnumerable<Thing> things)
{
    var sb = new StringBuilder();
    using (var stringWriter = new StringWriter(sb) )
    {
        using (var htmlWriter = new HtmlTextWriter(stringWriter))
        {                   
            foreach (var thing in things)
            {
                htmlWriter.Write(thing.Description);
            }
        }
    }   
}


public void Method2(IEnumerable<Thing> things)
{
    var thingsBuilder = new StringBuilder();
    foreach ( var thing in things)
        thingsBuilder.Append(thing.Description);
    var thingsText = thingsBuilder.ToString();
    var sb = new StringBuilder();
    using (var stringWriter = new StringWriter(sb) )
    {
        using (var htmlWriter = new HtmlTextWriter(stringWriter))
        {           
            htmlWriter.Write(thingsText);                   
        }
    }   
}

public class Thing
{
    public string Description {get; set;}
}
// Define other methods and classes here

(use linq pad)

enter image description here

, - 2 -. . Capacity StringBuilder, . , HtmlTextWriter , , .

+2

, , .

  • , .
  • , asp.net.
+2

<%= x %> TextWriter . , . .

, .

+1

Your colleague was right, I tried the same job (for example, added text to the word). I made a string using Stringbuilder and foreach, and only after it is added to the word does it work faster than when I try to add the string to foreach.

0
source

In addition, this will depend on whether the site has been precompiled.

A more important IMHO problem is the lack of maintainability, which will be introduced by moving the presentation into the code behind. For example, if I need to add a class to a div, I will not need to go into the code to do this.

0
source

All Articles