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) => y.Length.CompareTo(x.Length));
for (int i = 0; i < word.Length; i++)
{
string searchValue = word[i];
if (i <= MAX_WORD_ITERATIONS && (xmlsearchResults == BLANK_SEARCH_XML_SCHEMA || xmlsearchResults == string.Empty))
{
xmlsearchResults = GetCachedRecord(thisSearch, searchValue);
}
if (xmlsearchResults != BLANK_SEARCH_XML_SCHEMA && xmlsearchResults != string.Empty)
{
thisSearch.Value = searchValue;
ignoreCache = true;
break;
}
}
xmlString.Append(string.Format(@"<{0}>", xmlHeader));
int objcount = 0;
string prevResultName = string.Empty;
try
{
if (searchResults.hitCount > 0)
{
List<SearchItem> SearchResults = new List<SearchItem>();
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") && code.Length <= MAX_CODE_LENGTH &&
name != prevResultName && objcount < 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 => s.Downloads).ToList();
for (int i = 0; i < SortedSearchResults.Count(); i++)
{
if ((objcount < maxResults))
{
string te = string.Format("<ResultName>{0}</ResultName>", SortedSearchResults[i].Description);
if (!xmlString.ToString().Contains(te))
{
xmlString.Append(SortedSearchResults[i].GetXMLString());
objcount += 1;
}
}
}
}
}
xmlString.Append(string.Format(@"</{0}>", xmlHeader));
return objcount;