4

I wrote a loop to display each line one by one from a List of string. The problem is that the list contains more that 45,000 lines and its taking a lot of time to create the page for displaying.

Can someone please help in optimizing the code !

        List<string> OverrrideLog = lc.getOverrideLog();
        List<string> AccessLog = lc.getAccessLog();

        foreach (string s in OverrrideLog)
            lblOverrideLog.Text += s + "<br/>";

        foreach (string s in AccessLog)
            lblAccessLog.Text += s + "<br/>";

Here lblOverrideLog and lblAccessLog are literals and each list has more than 22,000 lines.

5
  • 7
    use a StringBuilder! Commented Sep 1, 2011 at 11:33
  • 1
    use String.Join Method (String, String[]) Commented Sep 1, 2011 at 11:35
  • 2
    @CD No, String.Join Method (String, IEnumerable<String>). He already has a List` Commented Sep 1, 2011 at 11:44
  • xanatos: yes, you are right.. Commented Sep 1, 2011 at 11:48
  • read this article codeproject.com/KB/cs/StringBuilder_vs_String.aspx Commented Sep 1, 2011 at 12:05

4 Answers 4

5

You can use the String.Join Method (String, IEnumerable):

List<string> OverrrideLog = lc.getOverrideLog();
List<string> AccessLog = lc.getAccessLog();

lblOverrideLog.Text = String.Join("<br />", OverrrideLog);
lblAccessLog.Text = String.Join("<br />", AccessLog);

(See also String.Join vs. StringBuilder: which is faster?)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot :) I used this one and the one answered by SBlackler. Both are working great !
Admittedly, I didn't know about String.Join in this way +1
4

Untested but try this:

List<string> OverrrideLog = lc.getOverrideLog();
List<string> AccessLog = lc.getAccessLog();

StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();

foreach(var el in OverrrideLog)
{
  sb.Append(el);
  sb.Append(" <br />");
}

foreach(var el in AccessLog)
{
  sb2.Append(el);
  sb2.Append(" <br />");
}

lblOverrideLog.Text = sb.ToString();
lblAccessLog.Text = sb2.ToString();

Edit:

woops, put val instead of var

1 Comment

Thanks a lot :) I used this one and the one answered by CD. Both are working great
0

You should be using a StringBuilder.

Concatenating strings like you are creates thousands of temporary ones in memory.

1 Comment

George, sorry: I red your comment and started to write an answer... but you are the first, so do you prefer I delete my answer? :)
0

for a exsample

            OverrrideLog.ForEach(x=>{
                if (sbOverrideLog.Length > 0)
                    sbOverrideLog.Append("<br />");

                sbOverrideLog.Append(x);
            });

            AccessLog.ForEach(x =>
            {
                if (sbAccessLog.Length > 0)
                    sbAccessLog.Append("<br />");

                sbAccessLog.Append(x);
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.