Most efficient way to scan an array for a match

J

JezB

What is the most efficient way to scan an array for a match ? I could just
iterate directly through it comparing each array entry with what I am
looking for, I could use an enumerator to do the same thing (though I dont
know if this is better), I could convert the array to some other structure
which makes direct lookup possible (if I want to do the same thing many
times), or maybe some other entirely different approach is better. Any
ideas?
 
H

Hans Kesting

JezB said:
What is the most efficient way to scan an array for a match ? I could just
iterate directly through it comparing each array entry with what I am
looking for, I could use an enumerator to do the same thing (though I dont
know if this is better), I could convert the array to some other structure
which makes direct lookup possible (if I want to do the same thing many
times), or maybe some other entirely different approach is better. Any
ideas?

That would depend on the type of data and the nature of the lookup.
Provide some details and maybe we can help you further.

Hans Kesting
 
J

JezB

I'm trying to test whether a given culture identifier (eg. "En-GB") is in
the list of supported cultures, which I obtain using
CultureInfo[] allCultures =
CultureInfo.GetCultures(CultureTypes.AllAvailableCultures);
This returns an array of CultureInfo's, which I want to scan for a given
identifier.
 
J

JezB

I'm using the enumerator method at the moment, but I want to make sure it's
the most efficient way

string searchCultureID = "en-GB";
CultureInfo[] allCultures =
CultureInfo.GetCultures(CultureTypes.AllAvailableCultures);
System.Collections.IEnumerator cultureEnumerator =
allCultures.GetEnumerator();
bool found = false;
cultureEnumerator.Reset();
while (cultureEnumerator.MoveNext() && !found)
{
if (((CultureInfo)cultureEnumerator.Current).ToString() ==
searchCultureID)
found = true;
}

JezB said:
I'm trying to test whether a given culture identifier (eg. "En-GB") is in
the list of supported cultures, which I obtain using
CultureInfo[] allCultures =
CultureInfo.GetCultures(CultureTypes.AllAvailableCultures);
This returns an array of CultureInfo's, which I want to scan for a given
identifier.


Hans Kesting said:
That would depend on the type of data and the nature of the lookup.
Provide some details and maybe we can help you further.

Hans Kesting
 
H

Hans Kesting

JezB said:
I'm using the enumerator method at the moment, but I want to make sure it's
the most efficient way

string searchCultureID = "en-GB";
CultureInfo[] allCultures =
CultureInfo.GetCultures(CultureTypes.AllAvailableCultures);
System.Collections.IEnumerator cultureEnumerator =
allCultures.GetEnumerator();
bool found = false;
cultureEnumerator.Reset();
while (cultureEnumerator.MoveNext() && !found)
{
if (((CultureInfo)cultureEnumerator.Current).ToString() ==
searchCultureID)
found = true;
}

What you could try (I don't know about performance!)
1) sort the array by using the Sort() method and a custom IComparer.Compare method
2) search with the BinarySearch() method (And a custom IComparer)

If you need this often, you can increase performance by caching the sorted array.

Hans Kesting
 
S

Scott Allen

Hi Jez:

A short and quick optimization would be to use a for loop instead of
an enumerator, because indexing into the array is generally faster
than going through the method calls and properties of an enumerator.

A better optimization would be to use Array.Sort to sort the array on
the culture ID (you'll need to write an object implementing IComparer
to do this). Then use Array.BinarySearch to find the culture ID.
 
R

Ravenwolf

What is the most efficient way to scan an array for a match ? I could
just iterate directly through it comparing each array entry with what
I am looking for, I could use an enumerator to do the same thing
(though I dont know if this is better), I could convert the array to
some other structure which makes direct lookup possible (if I want to
do the same thing many times), or maybe some other entirely different
approach is better. Any ideas?

The best way is to use a collection like hashtable or arraylist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top