Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4260092
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:52:25+00:00 2026-05-21T05:52:25+00:00

This is the code that is causing me problems: private string buildHTMLTree() { Dictionary<string,

  • 0

This is the code that is causing me “problems”:

private string buildHTMLTree()
{
    Dictionary<string, string> parents = new Dictionary<string, string>();
    Dictionary<string, string> childs = new Dictionary<string, string>();
    ArrayList array = new ArrayList();
    array = simulateInput();

    string html = "";

    foreach (KeywordRows kwd in array)
    {
        if (kwd.root_keyword == kwd.keyword)
        {
            if (!parents.ContainsKey(kwd.keyword))
                parents.Add(kwd.keyword, kwd.root_keyword);
        }
        else
        {
            if (!childs.ContainsKey(kwd.keyword))
                childs.Add(kwd.keyword, kwd.root_keyword);
        }
    }

    html += "<ul id=\"parents\">";
    foreach (string parent in parents.Values)
    {
        html += "<li id=\"" + parent + "\">" + parent;

        if (childs.ContainsValue(parent))
        {
            html += "<ul id=\"parents\">";
            process(ref childs, ref html, parent);
            html += "</ul>";
        }

        html += "</li>";
    }
    html += "</ul>";

    return Properties.Resources.htmlTree_tmpl.Replace("{KEYWORDS}", html);
}

public void process(ref Dictionary<string, string> _childs, ref string _html, string parent)
{
    var getChilds =
    from o in _childs
    where (o.Value == parent)
    select o.Key;

    foreach (var tmp in getChilds)
    {
        string child = tmp.ToString();

        if (_childs.ContainsValue(child))
        {
            _html += "<li id=\"" + child + "\">" + child + "<ul id=\"" + child + "\">";
            process(ref _childs, ref _html, child);
            _html += "</ul></li>";
        }
        else
        {
            _html += "<li id=\"" + child + "\">" + child + "</li>";
        }
    }

    return;
}

public class KeywordRows
{
    private string _keyword;
    private string _root_keyword;

    public KeywordRows(string keyword, string root_keyword)
    {
        _keyword = keyword;
        _root_keyword = root_keyword;
    }

    public string keyword
    {
        get
        {
            return _keyword;
        }
        set
        {
            _keyword = value;
        }
    }

    public string root_keyword
    {
        get
        {
            return _root_keyword;
        }
        set
        {
            _root_keyword = value;
        }
    }
}

}

My problem is that I have this function I use to turn a 2 columns list of data to a nested html tree, the problem is that when this data contains a lot of “rows”, the function takes forever to finish, I didn’t got any exception but I let it ran with a 100k rows data as input for like 15 minutes and it didn’t finished.

I’ve put together a small Visual Studio 2010 project to show the problem, it simulates an input of X size and executes the function.

Here’s the project: http://www.fileden.com/files/2011/4/10/3112563//HTMLTreeTest.zip

What can I do to do my code (much) faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:52:26+00:00Added an answer on May 21, 2026 at 5:52 am

    The posted code is likely to be slow due to string concatenation. Use a StringBuilder instead, or the more specialized HtmlTextWriter.
    Here’s your code with a StringBuilder, which works in saner time:
    http://pastebin.com/0VrM6SLV

    You may also consider writing directly to your StreamWriter – this avoids having the whole string in memory at once, which is a good idea for a very large string.
    The code is very similar to using the StringBuilder. We pass strWri, and use StreamWriter.Write:
    http://pastebin.com/5afYdWHM
    Note that I had to split your template string (the HTML wrapper) to make it work logically – we can’t use String.Replace if we aren’t dealing with a whole string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that performs an ajax call and loads the results into
So I have this code that takes care of command acknowledgment from remote computers,
I'm a newbie to python and the app engine. I have this code that
I have this jQuery code that queries an API on a keyup event (via
I have this legacy code that I am working with and there is code
Is there an easy way to modify this code so that the target URL
I have this small code library that I'm considering releasing into Open Source. I
Well, I have this bit of code that is slowing down the program hugely
I've got a problem with inheritance and generics. This is the code that illustrates
The code that I want to write is like this: void MethodOnThreadA() { for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.