473,763 Members | 7,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2067

"JezB" <je***********@ blueyonder.co.u k> wrote in message news:uJ******** ******@TK2MSFTN GP11.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.Get Cultures(Cultur eTypes.AllAvail ableCultures);
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******** ******@TK2MSFTN GP11.phx.gbl...

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

news:uJ******** ******@TK2MSFTN GP11.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.Get Cultures(Cultur eTypes.AllAvail ableCultures);
System.Collecti ons.IEnumerator cultureEnumerat or =
allCultures.Get Enumerator();
bool found = false;
cultureEnumerat or.Reset();
while (cultureEnumera tor.MoveNext() && !found)
{
if (((CultureInfo) cultureEnumerat or.Current).ToS tring() ==
searchCultureID )
found = true;
}

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:eF******** ******@TK2MSFTN GP12.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.Get Cultures(Cultur eTypes.AllAvail ableCultures);
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******** ******@TK2MSFTN GP11.phx.gbl...

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

news:uJ******** ******@TK2MSFTN GP11.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.u k> wrote in message news:%2******** ********@tk2msf tngp13.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.Get Cultures(Cultur eTypes.AllAvail ableCultures);
System.Collecti ons.IEnumerator cultureEnumerat or =
allCultures.Get Enumerator();
bool found = false;
cultureEnumerat or.Reset();
while (cultureEnumera tor.MoveNext() && !found)
{
if (((CultureInfo) cultureEnumerat or.Current).ToS tring() ==
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.Compa re 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.BinarySea rch to find the culture ID.

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

On Thu, 29 Jul 2004 13:11:33 +0100, "JezB"
<je***********@ blueyonder.co.u k> wrote:
string searchCultureID = "en-GB";
CultureInfo[] allCultures =
CultureInfo.Ge tCultures(Cultu reTypes.AllAvai lableCultures);


Nov 16 '05 #6
"JezB" <je***********@ blueyonder.co.u k> wrote in
news:uJ******** ******@TK2MSFTN GP11.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
1888
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 = ; values = "foo"; values = "bar"; values = "bundy";
15
2340
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
1851
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 display all records. Pls help. Thanks Set rs = Me.Recordset.Clone rs.FindFirst fmonth & " And " & fyear Me.Bookmark = rs.Bookmark Do Until rs.EOF rs.FindNext fmonth & " or " & fyear
6
1602
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, what is the most efficient method? Sincerely, José
11
3618
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 million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
6
1735
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 there any substitution class for enabling this? Please reply. Thanks in advance. Hyun-jik Bae
1
5737
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 work manually. I am looking for an automated way. Thank you, Max
2
7905
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
1505
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 function, followed by how many times it appears and the lines it appears on. What I was looking for was ways to improve, simplfy and beautify the code. More to aid my learning of Python than produce a perfect way to scan for this (netbeans was...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.