473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 2042

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will | last post by:
I have the following code: var rx = /{{(.+?)}}/i; var expr = 'each {{word}} wrapped in {{curly}} {{braces}} in this {{string}} needs to be {{replaced}} with a different {{value}}.'; var values...
15
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
2
by: Daniel Tan | last post by:
In my code, i tried to scan for each record in my database that matched certain criteria and then display it in a subform. I used the following codes but it will only display last record, i want to...
6
by: José Joye | last post by:
I have to compare 2 byte and I must be sure that they are fully identic. I have to perform this check about 1000 times per minute and on arrays that are between 100-200K in size. In that sense,...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
6
by: Hyun-jik Bae | last post by:
Is there any way how to get the item which has the most similar key in key-value collection object such as Dictionary<or SortedDictionary<> although there is no matching key? If there is none, is...
1
by: Maxwell2006 | last post by:
Hi, I am working with strongly typed datatables. What is the most efficient way to build a new DataTAble based on the result of DataTable.Select? At this point I use a foreach loop to do the...
2
by: chris | last post by:
I have a few byte arrays that I would like to combine into one array (order needs to be kept). What would be the most efficient way to do this? Thanks for your time, Chris
3
by: cokofreedom | last post by:
I've written up a little piece of code that isn't that foolproof to scan through a file (java presently) to find functions and then look for them throughout the document and output the name of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.