473,406 Members | 2,312 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,406 software developers and data experts.

Need fast ListOf(type comparison

!NoItAll
297 100+
I have a ListOf(Type that I've created that will often have well over 20-thousand entries.
The type I am placing into the list looks like this:

Expand|Select|Wrap|Line Numbers
  1. Structure MyType
  2.    Dim FileName as String
  3.    Dim FileDate as String
  4.    Dim FileSize as String
  5.    Dim NewPath as String
  6. End Structure
  7. Dim MyList as ListOf(MyType)
  8.  
  9.  
Before I add anything to the list I need to check and make sure it's not already in there:

Expand|Select|Wrap|Line Numbers
  1. If  MyList.Contains(sNewEntry) = False then
  2.    MyList.Add(sNewEntry)
  3. End if
  4.  
...but with anything above about 7-thousand entries it takes about 1/4 to 1/2 second for each comparison. With more than 15-thousand it takes nearly a second. If I am adding a total of 20-thousand records I could miss Christmas!

I *thought* it would be faster to try

Expand|Select|Wrap|Line Numbers
  1. If MyList.BinarySearch(sNewEntry) < 0 then
  2.    MyList.Add(sNewEntry)
  3. End if
  4.  
But this throws an exception ("failed to compare") - so either this is not what I want or I am just doing it wrong.

I also thought I might throw a hash into the structure and then force the CONTAINS or BINARYSEARCH method to only compare hashes - but I just can't figure out how to do that... Also - just how unique is the default hash?

Any suggestions would be very much appreciated!

If I stumble across a better solution I will be sure to post it here - I loath threads without answers!

Thanks all!
Dec 17 '09 #1

✓ answered by tlhintoq

So each list element is a struct of 4 items.
So each test of the compare is actually having to check if all 4 items in the list element are the same as your comparitor.
Meaning 5,000 element test is 20,000 {internal} comparisons.

If the struct were a class you could access the members individually.
MyType.FileName for example
You could the loop through testing any one element to see if its the same.
Now you would be doing 5,000 comparisons instead of 20,000

2 1175
tlhintoq
3,525 Expert 2GB
So each list element is a struct of 4 items.
So each test of the compare is actually having to check if all 4 items in the list element are the same as your comparitor.
Meaning 5,000 element test is 20,000 {internal} comparisons.

If the struct were a class you could access the members individually.
MyType.FileName for example
You could the loop through testing any one element to see if its the same.
Now you would be doing 5,000 comparisons instead of 20,000
Dec 17 '09 #2
!NoItAll
297 100+
Here's what I've done - it's not big trick, but it has improved performance dramatically. As tlhintoq pointed out there were way too many compares going on.
I added an additional element to the structure
Expand|Select|Wrap|Line Numbers
  1. Structure MyType
  2.    Dim FileName as String
  3.    Dim FileDate as String
  4.    Dim FileSize as String
  5.    Dim NewPath as String
  6.    Dim FileNameHash as Integer
  7. End Structure
  8. Dim MyList as ListOf(MyType)
  9.  
Then I wrote my own compare routine
Expand|Select|Wrap|Line Numbers
  1. Friend Function CompareHash(ByRef LongList as List(of MyType), NewItem as MyType) as Boolean
  2.  
  3. For Each item as MyType in LongList
  4.     If item.FileNameHash = NewItem.FileNameHash then
  5.            If Item.FileName = NewItem.FileName then
  6.                  Return True
  7.             End if
  8.      End if
  9. Next
  10. Return False
  11.  
This way I am only looking at the hash (an integer) so the checks will be very fast.
Notice that I check the filename even after there is a hash match. This is because the hash is not guarenteed unique. If I have two strings they will definately generate the exact same hash. But it is possible for two different strings to generate identical hashes - so we do a second compare after matching hashes are found to be sure. This is still hundreds of times faster than doing all string matches.
For my routine a list of 22-thousand items went from 5 minutes to 18 seconds.
Dec 17 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: George Sakkis | last post by:
Explicit type checking is not typically seen in Python code, and usually that's not a big problem; most typing errors are likely to raise a TypeError or AttributeError sooner than later. There are...
24
by: Xah Lee | last post by:
What is Expresiveness in a Computer Language 20050207, Xah Lee. In languages human or computer, there's a notion of expressiveness. English for example, is very expressive in manifestation,...
5
by: bittercold | last post by:
Hi, guys, I need to overload global "new" operator but I have to know the type id "new" is begin invoked on. For example if I overload it like this void *operator new(size_t n) { ...body......
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
3
by: john | last post by:
I have a situation where i have a base class and a sub-class. A null instance of the sub-class is passed into a function. The function needs to create a new instance of the sub-class, but the...
0
by: Martin Aliger | last post by:
Hi all, is there any way how to access type which is decorated with mine attribute within that attribute? E.g. public class Whatever {
5
by: gamehack | last post by:
Hi all, I need type sizes like C's int8_t, int16_t etc. Is there a portable C++ way or I just have to typedef according to the platform? Regards
11
by: solandre | last post by:
my motivation is as follows: i have to binarysearch several million times an long that is several million items big. this costs time, although i use Array.Binarysearch<long>. so i try to keep...
6
by: lothar.behrens | last post by:
Hi, I have some code, where I am using a variable for a given interface. One step later, using Remoting, I need Type.TypeOf("...") The following code returns Nothing, but the type above could...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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.