472,127 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Most efficient way to scan an array for a match

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?
Nov 16 '05 #1
6 1964

"JezB" <je***********@blueyonder.co.uk> wrote in message news:uJ**************@TK2MSFTNGP11.phx.gbl...
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
Nov 16 '05 #2
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.AllAvailableC ultures);
This returns an array of CultureInfo's, which I want to scan for a given
identifier.
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...

"JezB" <je***********@blueyonder.co.uk> wrote in message

news:uJ**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #3
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.AllAvailableC ultures);
System.Collections.IEnumerator cultureEnumerator =
allCultures.GetEnumerator();
bool found = false;
cultureEnumerator.Reset();
while (cultureEnumerator.MoveNext() && !found)
{
if (((CultureInfo)cultureEnumerator.Current).ToString () ==
searchCultureID)
found = true;
}

"JezB" <je***********@blueyonder.co.uk> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
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.AllAvailableC ultures);
This returns an array of CultureInfo's, which I want to scan for a given
identifier.
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...

"JezB" <je***********@blueyonder.co.uk> wrote in message

news:uJ**************@TK2MSFTNGP11.phx.gbl...
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


Nov 16 '05 #4

"JezB" <je***********@blueyonder.co.uk> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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.AllAvailableC ultures);
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
Nov 16 '05 #5
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.

--
Scott
http://www.OdeToCode.com

On Thu, 29 Jul 2004 13:11:33 +0100, "JezB"
<je***********@blueyonder.co.uk> wrote:
string searchCultureID = "en-GB";
CultureInfo[] allCultures =
CultureInfo.GetCultures(CultureTypes.AllAvailable Cultures);


Nov 16 '05 #6
"JezB" <je***********@blueyonder.co.uk> wrote in
news:uJ**************@TK2MSFTNGP11.phx.gbl:
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.
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Tor Erik Sřnvisen | last post: by
2 posts views Thread by Daniel Tan | last post: by
2 posts views Thread by chris | last post: by
3 posts views Thread by cokofreedom | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.