473,734 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fastest way to see if item exists in list

What's the fastest/easiest/best way to determine if a certain item exists
in a list?

(And what's the slowest/dumbest - I suppose that's enumerating the elements
and comparing each in turn in VB code?)

My collection can have up to a few thousand elements (each either a GUID or
an ID card number in "000-0000000-00" format) and an even larger number may
have to be checked, so speed does become a bit of an issue.
The shortest will be something like this, but how is the speed compared to
other methods?

Dim IDList As New Collection
IDList.Add(Noth ing, "a")
IDList.Add(Noth ing, "b")
IDList.Add(Noth ing, "c")

Public Function Exists(ID As String) As Boolean
Try
Dim Dummy As Object = IDList.Item(ID)
Return True
Catch
Return False
End Try
End Function

Nov 21 '05 #1
4 14981
Luc

In the way you use it now, I would have a look at the hashtable. Try to
avoid the VisualBasic Collection. It is a complete different collection than
all other Net collections.

hashtable
http://msdn.microsoft.com/library/de...classtopic.asp

After the scene is of course a lot done in the above collection. Maybe can
you think at the arraylist as well
http://msdn.microsoft.com/library/de...archtopic1.asp

The last method I never used.

However I hope this helps,

Cor
Nov 21 '05 #2
On Thu, 12 May 2005 15:35:00 +0200, "Cor Ligthert"
<no************ @planet.nl> wrote:
Luc

In the way you use it now, I would have a look at the hashtable. Try to
avoid the VisualBasic Collection. It is a complete different collection than
all other Net collections.


Thanks a lot.

The difference is astounding, a hashtable is 1000 times faster than the
method I used in my original post, probably due to the exception that
occurs upon every mismatch more than to the added overhead of putting it in
a function.

I wrote some test code that
- puts 10,000 strings in a VB collection
- looks up 10,000 random strings in it (as I did it in my original post),
- repeats the same with a hashtable and its Contains method.

Result:
- adding 10,000 elements to VB collection: 62 msec
- looking up 10,000 values in VB collection: 18363 msec
- adding 10,000 elements to hashtable: 0 msec
- looking up 10,000 values in hashtable: 15 msec.

The same strings were used for both tests.
The same random sequence was used in the lookup phase (reseeded random
generator to give the same sequence). The "strings" were integer.ToStrin g
with values from 1 to 20,000 for the list and random 10,000 to 29,999 for
the lookup values.

The latter two shouldn't be taken too seriously, the system clock accuracy
is only 15 msec.

Nov 21 '05 #3
"Lucvdv" <re**********@n ull.net> schrieb:
What's the fastest/easiest/best way to determine if a certain item exists
in a list?


\\\
If MyList.Contains (<object>) Then
...
End If
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
On Fri, 13 May 2005 15:42:36 +0200, "Herfried K. Wagner [MVP]"
<hi************ ***@gmx.at> wrote:
"Lucvdv" <re**********@n ull.net> schrieb:
What's the fastest/easiest/best way to determine if a certain item exists
in a list?


\\\
If MyList.Contains (<object>) Then
...
End If
///


I assume you're referring to an ArrayList: it's the simplest, but Cor's
suggestion of using a hashtable delivers lookup results 100 times faster.

Checking for 10,000 items in a list of 10,000 with 50% hits:

VB Collection: 14889.0049 msec
ArrayList: 1484.2135 msec
HashTable: 15.6233 msec

Initializing the list is faster with an ArrayList (as expected).

Code complexity is about the same:

Dim s As String = ...
HashTable.Add(s , Nothing)
versus
ArrayList.Add(s )

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1900
by: Helge | last post by:
I wonder how this can be accomplished: I've got a list, containing several strings. The strings look like this: I would like to create one object for each item in the list, and the name of the object should be "str(item.split("/"))".
1
2917
by: Timo | last post by:
I am trying to use the DropDownList_SelectedIndexChanged event on a dropdown which is dynamically populated with different values at runtime, depending on what the user has been doing. The dropdown works and the event fires except when the user selects the *first* item in the list, which appears to be the default selection, even though the generated HTML code does not have a "selected" property set. Is it possible to cause the...
4
7149
by: GJP | last post by:
Hello. I have two list boxes. One with a picture name, and one for the ID of that picture (both coming from a database). The user selects the name of the picture and presses a button, the picture and the picture ID are then moved to other list boxes. This works ok, but once this has been done, i dont want to delete the item from the origional listbox, i just want to deselect it, but i cant seem to do this.
5
17832
by: mabond | last post by:
Hi My project has a form, two list boxes (lstArchive and lstSchedule) and two buttons (btnSendArchive and btnSendSchedule) Each list box represents the content of a directory. The command buttons move the selected item out of one directory and into the other and refreshes the list boxes to reflect the change in directory contents I now want to add "context menu" functionality to the list boxes to preform
4
27899
by: Vivek | last post by:
How do I update a current item in the LIST<T>? Thanks
2
1006
by: nime | last post by:
Hi! If a collection ( here; a List) is Nothing then I cannot use .Add method, It causes an error: System.NullReferenceException was unhandled Message="Object reference not set to an instance of an object." First I try .Add method, then I try to create an instance and use it, if error occurs. Am I doing it right or is there a better way to resolve this error?
6
8968
by: ahlongxp | last post by:
list=('a','d','c','d') for a in list: if a=='a' : #skip the letter affer 'a' what am I supposed to do?
6
9597
patjones
by: patjones | last post by:
Sometimes you might have two list boxes side by side, and want to double-click on an item in one box to move it to the other. For instance, maybe the first list box contains a list of customers, and you want to perform some function (say, email them) that pertains to only a few of those customers. By double-clicking on the customer names you want, you get the desired list in the right hand box. First, make a generic sub that takes two...
2
2434
by: aberry | last post by:
I have strange issue when deleting element of list which I'm browsing. Here is the code , please have a look. >>> li = >>> for name in li: print name mike anil jassi >>> for name in li:
0
8951
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
8783
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,...
0
9317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
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,...
0
9187
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6743
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
6040
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();...
1
3266
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
2
2734
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.