-
Compare Array Without Loop
I have done some searching and from what I have read I don't think this is possible, but I thought I would ask anyway.
Is there a way to do this:
VB Code:
Dim a(2) As Boolean
Dim b(2) As Boolean
a(0) = False
a(1) = True
a(2) = True
b(0) = False
b(1) = True
b(2) = True
If a = b Then MsgBox("yay")
-
Re: Compare Array Without Loop
not in VB6..... in .NET it can be done... but even that takes some code writing.
Tg
-
Re: Compare Array Without Loop
Who says you can't in VB6? Using a UDT should help :bigyello:
VB Code:
Option Explicit
Private Type tTest
Matrix(2) As Long
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As String, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_Activate()
Dim A As tTest, B As tTest
A.Matrix(0) = 5
A.Matrix(1) = 10
A.Matrix(2) = 15
MsgBox TypeEquals(A, B) ' False
B.Matrix(0) = 5
B.Matrix(1) = 10
B.Matrix(2) = 15
MsgBox TypeEquals(A, B) ' True
End Sub
Private Function TypeEquals(A As tTest, B As tTest) As Boolean
Dim StrA As String, StrB As String
StrA = String(Len(A), 0)
CopyMemory StrA, A, Len(A)
StrB = String(Len(A), 0)
CopyMemory StrB, B, Len(A)
TypeEquals = StrA = StrB
End Function
-
Re: Compare Array Without Loop
Even Better. Don't need UDT's after all!!!
VB Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As String, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_Activate()
Dim A(2) As Long, B(2) As Long
A(0) = 5
A(1) = 10
A(2) = 15
MsgBox ArrayEquals(A, B) ' False
B(0) = 5
B(1) = 10
B(2) = 15
MsgBox ArrayEquals(A, B) ' True
End Sub
Private Function ArrayEquals(A() As Long, B() As Long) As Boolean
Dim StrA As String, StrB As String
StrA = String(Len(A(0)) * UBound(A), 0)
CopyMemory StrA, A(0), Len(A(0)) * UBound(A)
StrB = String(Len(A(0)) * UBound(A), 0)
CopyMemory StrB, B(0), Len(A(0)) * UBound(A)
ArrayEquals = StrA = StrB
End Function
-
Re: Compare Array Without Loop
Don't forget ...
VB Code:
If VB cant do it Then
Do it Yourself
End If
Here's a quikly put together module that you could reuse in any application and it accepts 2 arrays of any type.
VB Code:
Public Function CompareArray(ParamArray Arrays()) As Boolean
Dim A1() As Variant
Dim A2() As Variant
Dim i As Long
ReDim A1(UBound(Arrays(0)))
ReDim A2(UBound(Arrays(1)))
For i = 0 To UBound(Arrays(0))
A1(i) = Arrays(0)(i)
Next
For i = 0 To UBound(Arrays(1))
A2(i) = Arrays(1)(i)
Next
Dim A1Size As Long
Dim A2Size As Long
A1Size = UBound(A1)
A2Size = UBound(A2)
If A1Size = A2Size Then
For i = 0 To A1Size
If A1(i) = A2(i) Then
CompareArray = True
Else
CompareArray = False
Exit For
End If
Next
Else
CompareArray = False
End If
End Function
Then just call it as so...
VB Code:
Private Sub Command1_Click()
Dim A(2) As Boolean
Dim B(2) As Boolean
A(0) = True
A(1) = False
A(2) = True
B(0) = True
B(1) = False
B(2) = True
[B]Label1.Caption = CompareArray(A, B)[/B]
End Sub
Cheers Hojo
-
Re: Compare Array Without Loop
My way is faster. No For loop needed and it compares it instantaniously. And he's looking for speed. ;)
Imagine if there are 100000 elements. How long do you think your function will run compared to mine?
-
Re: Compare Array Without Loop
I get from Post 1 that he is after ease of coding therefore not having to code a loop every time he wants to compare arrays.
Your way is limited in that you have to recode each time you have a different array type, mine will accept any. Plus if he has to code that much to avoid coding a loop he might as well code a loop.
If speed is an issue then perhaps a migration of our two method would be the optimum solution.
Hojo
-
Re: Compare Array Without Loop
I just ran your little test and As soon as i press the compare button the label updates with the answer immediatly. (with 100000 samples in each array)
-
Re: Compare Array Without Loop
Quote:
Originally Posted by Hojo
........updates with the answer immediatly.
Yeh, but Hojo, it does help that your using a "CRAY" computer :)
-
Re: Compare Array Without Loop
There are super computers and there are super programmers.
lol just kidding :D
Just a plain few year old desktop.
-
Re: Compare Array Without Loop
Not wanting to harp on this topic but i just relooked at my code and trimmed out the unneeded parts (this should also halve processing time)
VB Code:
Public Function CompareArray(ParamArray Arrays()) As Boolean
Dim i As Long
If UBound(Arrays(0)) = UBound(Arrays(1)) Then
For i = 0 To UBound(Arrays(0))
If Arrays(0)(i) = Arrays(1)(i) Then
CompareArray = True
Else
CompareArray = False
Exit For
End If
Next
Else
CompareArray = False
End If
End Function
Cheers Hojo
-
Re: Compare Array Without Loop
The fastest way to compare anything that can't be done directly in VB is to use RtlCompareMemory.
VB Code:
Declare Function RtlCompareMemory Lib "ntdll.dll" ( _
ByRef lpvSrc1 As Any, _
ByRef lpvSrc2 As Any, _
ByVal cbLen As Long _
) As Long
' Usage:
If (RtlCompareMemory(a(0), b(0), UBound(a) * LenB(a)) = UBound(a) * LenB(a)) Then
MsgBox "yay"
End If
-
Re: Compare Array Without Loop
But that's only supported for XP computers. I don't have that ;)
My code in post #4 also works instantaniously.
-
Re: Compare Array Without Loop
Thanks for all the help guys.
To clear things up: I am not looking for ease of coding (at the moment). I am only look for speed. I will run some speed test on the ideas you guys mentioned and see which works better in my situation. I figured copy memory had something to do with it, I just didn't know hot to use it.
BTW - my array will only have 9 elements in it. (Can anyone guess what I am making? ;))
-
Re: Compare Array Without Loop
A suduku solver? :ehh: :p
-
Re: Compare Array Without Loop
Noooooooooo....
Yeeeeeeeeees!!!!
;)
-
Re: Compare Array Without Loop
Good. Then post #4 is the fastest method. No performance test necessary. It's common sense. ;)
-
Re: Compare Array Without Loop
Why wouldn't penagate's be faster (post # 12)? Strings are very slow in VB. I want to avoid them at all costs.
-
Re: Compare Array Without Loop
VB Code:
Option Explicit
Private Declare Function RtlCompareMemory Lib "ntdll.dll" ( _
ByRef lpvSrc1 As Any, _
ByRef lpvSrc2 As Any, _
ByVal cbLen As Long _
) As Long
Private Sub Form_Load()
Dim a(2) As Boolean
Dim b(2) As Boolean
a(0) = False
a(1) = True
a(2) = True
b(0) = False
b(1) = True
b(2) = True
lLen = UBound(a) * LenB(a)
If (RtlCompareMemory(a(0), b(0), lLen) = lLen) Then
MsgBox "yay"
End If
End Sub
I get this error: Compile error - Variable required - Can't assign to this expression
... and it highlights the the a withing LenB(a).
Any ideas?
-
Re: Compare Array Without Loop
RtlCompareMemory is not in all OS's. Just XP I believe. I can't use it cause I don't have the supported API in my ntdll.dll file.
And my function only assigns values to a string twice. So there shouldnt be any impact on performance.
-
Re: Compare Array Without Loop
Well, I hope whoever scores the sudoku entries has XP. :D. I think I will go mention that now in the sudoku thread.
I think I am going to use penagate's, because it is easier and seems quicker.
I fixed it also. I need to Dim lLen and change LenB(a) to LenB(a(0)) and it works now.
-
Re: Compare Array Without Loop
Whoops, my bad.
ntdll.dll, as one might expect, is part of all NT-based Windows', but will work also on Win 9x, if supplied.
-
Re: Compare Array Without Loop
It's supplied, just not the RtlCompareMemory function in the dll file, unless downloaded from a dll site that contains hundreds of dll files to choose from that you can download for free. Everyone with earlier versions of Windows using your program would have to upgrade that file then, unless supplied by you.
-
Re: Compare Array Without Loop
I'll grab a copy and throw it in whenever I zip up the project.
Thanks guys.