472,353 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(Nothing, "a")
IDList.Add(Nothing, "b")
IDList.Add(Nothing, "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 14438
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.ToString
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**********@null.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**********@null.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
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...
1
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,...
4
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...
5
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...
4
by: Vivek | last post by:
How do I update a current item in the LIST<T>? Thanks
2
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...
6
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
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,...
2
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:...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.