Fastest way to compare arrays

L

Lance

What is the fastest way to determine if two arrays that
contain ValueTypes are equal? For example, lets say I
have the following:

Dim pt1 as New Drawing.Point(1, 2)
Dim pt2 as New Drawing.Point(2, 3)
Dim pt3 as New Drawing.Point(3, 4)
Dim pt4 as New Drawing.Point(4, 5)

Dim A() as Drawing.Point = {pt1, pt2, pt3}
Dim B() as Drawing.Point = {pt1, pt2, pt3}
Dim C() as Drawing.Point = {pt2, pt3, pt4}
Dim D() as Drawing.Point = {pt3, pt2, pt1}

What is the fastest way to determine that B equals A but
that C and D do not equal A? The only way I know is to
loop through the arrays and test each member (e.g.,
Drawing.Point.op_Equality(A(i), B(i)), but it seems like
there must be a better way.

Thanks for any help!
 
A

Armin Zingler

Lance said:
What is the fastest way to determine if two arrays that
contain ValueTypes are equal? For example, lets say I
have the following:

Dim pt1 as New Drawing.Point(1, 2)
Dim pt2 as New Drawing.Point(2, 3)
Dim pt3 as New Drawing.Point(3, 4)
Dim pt4 as New Drawing.Point(4, 5)

Dim A() as Drawing.Point = {pt1, pt2, pt3}
Dim B() as Drawing.Point = {pt1, pt2, pt3}
Dim C() as Drawing.Point = {pt2, pt3, pt4}
Dim D() as Drawing.Point = {pt3, pt2, pt1}

What is the fastest way to determine that B equals A but
that C and D do not equal A? The only way I know is to
loop through the arrays and test each member (e.g.,
Drawing.Point.op_Equality(A(i), B(i)), but it seems like
there must be a better way.

That's also the only way I know.
 
D

Daniel Bass

assuming { 1, 2, 3 } does not equal { 1, 3, 2 } (i.e. order matters in the
equality)
the only way to confirm to arrays are equal is to check every member...
however, you can note that two arrays are not equal as soon as two adjacent
members are not equal.

something like:

Function AreArraysEqual(ByRef one As Object(), ByRef two As Object()) As
Boolean

' check lengths are the same
If one.Length <> two.Length Then
Return False
End If

' check each element
For index As Integer = 0 To one.Length - 1
If (one.GetValue(index) <> two.GetValue(index)) Then
Return False
End If
Next

Return True
End Function



What is the fastest way to determine if two arrays that
contain ValueTypes are equal? For example, lets say I
have the following:

Dim pt1 as New Drawing.Point(1, 2)
Dim pt2 as New Drawing.Point(2, 3)
Dim pt3 as New Drawing.Point(3, 4)
Dim pt4 as New Drawing.Point(4, 5)

Dim A() as Drawing.Point = {pt1, pt2, pt3}
Dim B() as Drawing.Point = {pt1, pt2, pt3}
Dim C() as Drawing.Point = {pt2, pt3, pt4}
Dim D() as Drawing.Point = {pt3, pt2, pt1}

What is the fastest way to determine that B equals A but
that C and D do not equal A? The only way I know is to
loop through the arrays and test each member (e.g.,
Drawing.Point.op_Equality(A(i), B(i)), but it seems like
there must be a better way.

Thanks for any help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top