Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to make my recursive function work faster
    text
    copied!<p>This is the code that is causing me "problems":</p> <pre><code>private string buildHTMLTree() { Dictionary&lt;string, string&gt; parents = new Dictionary&lt;string, string&gt;(); Dictionary&lt;string, string&gt; childs = new Dictionary&lt;string, string&gt;(); 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 += "&lt;ul id=\"parents\"&gt;"; foreach (string parent in parents.Values) { html += "&lt;li id=\"" + parent + "\"&gt;" + parent; if (childs.ContainsValue(parent)) { html += "&lt;ul id=\"parents\"&gt;"; process(ref childs, ref html, parent); html += "&lt;/ul&gt;"; } html += "&lt;/li&gt;"; } html += "&lt;/ul&gt;"; return Properties.Resources.htmlTree_tmpl.Replace("{KEYWORDS}", html); } public void process(ref Dictionary&lt;string, string&gt; _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 += "&lt;li id=\"" + child + "\"&gt;" + child + "&lt;ul id=\"" + child + "\"&gt;"; process(ref _childs, ref _html, child); _html += "&lt;/ul&gt;&lt;/li&gt;"; } else { _html += "&lt;li id=\"" + child + "\"&gt;" + child + "&lt;/li&gt;"; } } 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; } } } </code></pre> <p>}</p> <p>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.</p> <p>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.</p> <p>Here's the project: <a href="http://www.fileden.com/files/2011/4/10/3112563//HTMLTreeTest.zip" rel="nofollow">http://www.fileden.com/files/2011/4/10/3112563//HTMLTreeTest.zip</a></p> <p>What can I do to do my code (much) faster?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload