patterncsharpMinor
Complex search program
Viewed 0 times
searchprogramcomplex
Problem
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(@"", xmlHeader));
int objcount = 0;
string prevResultName = string.Empty;
try
{
if (searchResults.hitCount > 0)
{
List SearchResults = new List();
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 s.Downloads).ToList();
for (int i = 0; i {0}", SortedSearchResults[i].Description);
if (!xmlString.ToString().Contains(te))
{
xmlString.Append(SortedSearchResults[i].GetXMLString());
objcount += 1;
}
}
}
}
}
xmlString.Append(string.Format(@"", xmlHeader));
return objcount;Solution
Comparing strings case insensitively is done by using the proper overload:
By calling
In your case you use
It's prettier to use
Write your variable names in full:
Constants are
The
Why not this:
You can clean this up a bit more but I'm not in a boolean algebra mood.
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) >= 0It'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 && !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.
Code Snippets
string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase)str4.IndexOf("dyt2", StringComparison.OrdinalIgnoreCase) >= 0Boolean 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;
}if (!(thisSearch.FilterDescription && !name.ToLower().Contains(thisSearch.Value.ToLower()))
{
SearchItem oSearch = new SearchItem(name, code);
SearchResults.Add(oSearch);
prevResultName = name;
}Context
StackExchange Code Review Q#83934, answer score: 8
Revisions (0)
No revisions yet.