Speeding up a search .net 4.0
- by user231465
Wondering if I can speed up the search.
I need to build a functionality that has to be used by many UI screens
The one I have got works but I need to make sure I am implementing a fast algoritim if you like
It's like an incremental search.
User types a word to search for eg
const string searchFor = "Guinea";
const char nextLetter = ' '
It looks in the list and returns 2 records
"Guinea and Guinea Bissau "
User types a word to search for eg
const string searchFor = "Gu";
const char nextLetter = 'i'
returns
3 results.
This is the function but I would like to speed it up.
Is there a pattern for this kind of search?
class Program
{
static void Main()
{
//Find all countries that begin with string + a possible letter added to it
//const string searchFor = "Guinea";
//const char nextLetter = ' '; //returns 2 results
const string searchFor = "Gu";
const char nextLetter = 'i';
List<string> result = FindPossibleMatches(searchFor, nextLetter);
result.ForEach(x=>Console.WriteLine(x)); //returns 3 results
Console.Read();
}
/// <summary>
/// Find all possible matches
/// </summary>
/// <param name="searchFor">string to search for</param>
/// <param name="nextLetter">pretend user as just typed a letter</param>
/// <returns></returns>
public static List<string> FindPossibleMatches (string searchFor, char nextLetter)
{
var hashedCountryList = new HashSet<string>(CountriesList());
var result=new List<string>();
IEnumerable<string> tempCountryList = hashedCountryList.Where(x => x.StartsWith(searchFor));
foreach (string item in tempCountryList)
{
string tempSearchItem;
if (nextLetter == ' ')
{
tempSearchItem = searchFor;
}
else
{
tempSearchItem = searchFor + nextLetter;
}
if(item.StartsWith(tempSearchItem))
{
result.Add(item);
}
}
return result;
}
/// <summary>
/// Returns list of countries.
/// </summary>
public static string[] CountriesList()
{
return new[]
{
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
"Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
"Bolivia", "Bosnia Hercegovina", "Botswana", "Bouvet Island", "Brazil",
"Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Byelorussian SSR",
"Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
"Central African Republic", "Chad", "Chile", "China", "Christmas Island",
"Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands",
"Costa Rica", "Cote D'Ivoire", "Croatia", "Cuba", "Cyprus",
"Czech Republic", "Czechoslovakia", "Denmark", "Djibouti", "Dominica",
"Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador",
"England", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
"Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana",
"Gibraltar", "Great Britain", "Greece", "Greenland", "Grenada",
"Guadeloupe", "Guam", "Guatemela", "Guernsey", "Guiana",
"Guinea", "Guinea Bissau", "Guyana", "Haiti", "Heard Islands",
"Honduras", "Hong Kong", "Hungary", "Iceland", "India",
"Indonesia", "Iran", "Iraq", "Ireland", "Isle Of Man",
"Israel", "Italy", "Jamaica", "Japan", "Jersey",
"Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, South",
"Korea, North", "Kuwait", "Kyrgyzstan", "Lao People's Dem. Rep.", "Latvia",
"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein",
"Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar",
"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Mariana Islands", "Marshall Islands", "Martinique", "Mauritania", "Mauritius",
"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco",
"Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar",
"Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"Neutral Zone", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
"Nigeria", "Niue", "Norfolk Island", "Northern Ireland", "Norway",
"Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Pitcairn", "Poland",
"Polynesia", "Portugal", "Puerto Rico", "Qatar", "Reunion",
"Romania", "Russian Federation", "Rwanda", "Saint Helena", "Saint Kitts",
"Saint Lucia", "Saint Pierre", "Saint Vincent", "Samoa", "San Marino",
"Sao Tome and Principe", "Saudi Arabia", "Scotland", "Senegal", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Georgia", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden",
"Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikista", "Tanzania",
"Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu",
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States",
"Uruguay", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela",
"Vietnam", "Virgin Islands", "Wales", "Western Sahara", "Yemen",
"Yugoslavia", "Zaire", "Zambia", "Zimbabwe"
};
}
}
}
Any suggestions?
Thanks