Fastest way to compare arrays

  • Thread starter Thread starter Lance
  • Start date Start date
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!
 
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.
 
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!
 
Back
Top