Complex search program I am working on a fairly complex (at least it feels complex to me at the moment) search program and am looking to possibly increase the performance. Everything works exactly how I want it to, but I'm just wondering if there are any slight performance increases I could benefit from. In this code, even minor performance increases are essential.  This is due to the extensive amount of operations being performed.

// Split search into multiple terms and check the {x} longest terms against the cache.
string[] word = searchTerm.Split(' ');
Array.Sort(word, (x, y) =&gt; y.Length.CompareTo(x.Length));
for (int i = 0; i &lt; word.Length; i++)
{
    string searchValue = word[i];
    if (i &lt;= MAX_WORD_ITERATIONS &amp;&amp; (xmlsearchResults == BLANK_SEARCH_XML_SCHEMA || xmlsearchResults == string.Empty))
    {
        xmlsearchResults = GetCachedRecord(thisSearch, searchValue);
    }
    if (xmlsearchResults != BLANK_SEARCH_XML_SCHEMA &amp;&amp; xmlsearchResults != string.Empty)
    {
        thisSearch.Value = searchValue;
        ignoreCache = true;
        break;
    }
}




xmlString.Append(string.Format(@"&lt;{0}&gt;", xmlHeader));
int objcount = 0;
string prevResultName = string.Empty;
try
{
    if (searchResults.hitCount &gt; 0)
    {
        List&lt;SearchItem&gt; SearchResults = new List&lt;SearchItem&gt;();
        foreach (Node ResultNode in Results.Nodes)
        {
            string code = ResultNode.code.ToString();
            string result = ResultNode.id.ToString();
            string name = ResultNode.name_l.ToString();
            string image = string.Empty;

            if (!code.ToLower().Contains("ccb") &amp;&amp; code.Length &lt;= MAX_CODE_LENGTH &amp;&amp;
                name != prevResultName &amp;&amp; objcount &lt; maxResults)
            {
                Boolean addResult = true;
                if (thisSearch.FilterDescription)
                {
                    if (!name.ToLower().Contains(thisSearch.Value.ToLower()))
                    {
                        addResult = false;
                    }
                }
                if (addResult)
                {
                    SearchItem oSearch = new SearchItem(name, code);
                    SearchResults.Add(oSearch);
                    prevResultName = name;
                }
            }
        }
        var SortedSearchResults = SearchResults.OrderByDescending(s =&gt; s.Downloads).ToList();
        for (int i = 0; i &lt; SortedSearchResults.Count(); i++)
        {
            if ((objcount &lt; maxResults))
            {
                string te = string.Format("&lt;ResultName&gt;{0}&lt;/ResultName&gt;", SortedSearchResults[i].Description);
                if (!xmlString.ToString().Contains(te))
                {
                    xmlString.Append(SortedSearchResults[i].GetXMLString());
                    objcount += 1;
                }
            }
        }
    }
}
xmlString.Append(string.Format(@"&lt;/{0}&gt;", xmlHeader));
return objcount;
Comparing strings case insensitively is done by using the proper overload:

string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase)


By calling .ToLower() for each string to compare (4 times for each iteration), you create 4 new strings each time. If you have a lot of (long) strings, this will create a lot of garbage for no reason.

In your case you use .Contains() so you can mimic this by using .IndexOf():

str4.IndexOf("dyt2", StringComparison.OrdinalIgnoreCase) &gt;= 0




It's prettier to use bool instead of Boolean, partly because it also keeps your code consistent.



Write your variable names in full: objectCount, previousResultName, etc.



Constants are PascalCase, not WHATEVER_THIS_IS as in Java.



The addResult variable seems a little wonky. Instead of this:

Boolean addResult = true;
if (thisSearch.FilterDescription)
{
    if (!name.ToLower().Contains(thisSearch.Value.ToLower()))
    {
        addResult = false;
    }
}
if (addResult)
{
    SearchItem oSearch = new SearchItem(name, code);
    SearchResults.Add(oSearch);
    prevResultName = name;
}


Why not this:

if (!(thisSearch.FilterDescription &amp;&amp; !name.ToLower().Contains(thisSearch.Value.ToLower()))
{
    SearchItem oSearch = new SearchItem(name, code);
    SearchResults.Add(oSearch);
    prevResultName = name;
}


You can clean this up a bit more but I'm not in a boolean algebra mood.