QB64 Phoenix Edition
CompareMem - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26)
+---- Forum: SMcNeill (https://qb64phoenix.com/forum/forumdisplay.php?fid=29)
+---- Thread: CompareMem (/showthread.php?tid=2088)



CompareMem - SMcNeill - 10-13-2023

A little routine to quickly compare one mem block to another to see if they're identical or not.

Code: (Select All)
Declare CustomType Library
    Function memcmp% (ByVal s1%&, Byval s2%&, Byval n As _Offset)
End Declare

Randomize Timer

Screen _NewImage(1280, 720, 32)

'let's make this an unique and pretty image!
For i = 1 To 100
    Line (Rnd * _Width, Rnd * _Height)-(Rnd * width, Rnd * _Height), &HFF000000 + Rnd * &HFFFFFF, BF
Next

image1 = _CopyImage(0) 'identical copies for testing
image2 = _CopyImage(0) 'identical copy...  BUT
_Dest image2
PSet (Rnd * _Width, Rnd * _Height), &HFF000000 + Rnd * &HFFFFFF 'We've just tweaked it so that there's no way in hell it's the same as the other two now!
_Dest 0 'image3 is EXACTLY one pixel different from the other two.  Can we detect that?
image3 = _CopyImage(0) 'an identical copy once again, because 0 will change once we print the resul


Dim m(3) As _MEM
m(0) = _MemImage(0)
m(1) = _MemImage(image1)
m(2) = _MemImage(image2)
m(3) = _MemImage(image3)

result1 = CompareMem(m(0), m(1))
result2 = CompareMem(m(0), m(2))
result3 = CompareMem(m(1), m(2))

Print "Current Screen and Image 1 Compare:  "; result1
Print "Current Screen and Image 2 Compare:  "; result2
Print "Image1 and Image 2 Compare        :  "; result3

Print
Print "Press <ANY KEY> for a speed test!"
Sleep

t# = Timer
Limit = 1000
For i = 1 To Limit
    result = CompareMem(m(1), m(2))
    result = CompareMem(m(1), m(3))
Next
Print
Print Using "####.####### seconds to do"; Timer - t#;
Print Limit * 2; "comparisons of"; m(0).SIZE; "bytes."

Function CompareMem& (m1 As _MEM, m2 As _MEM)
    $Checking:Off
    If m1.SIZE <> m2.SIZE Then Exit Function 'not identical
    If m1.ELEMENTSIZE <> m2.ELEMENTSIZE Then Exit Function 'not identical
    If memcmp(m1.OFFSET, m2.OFFSET, m1.SIZE) = 0 Then x = -1 Else x = 0
    CompareMem = x
    $Checking:On
End Function

Demo here uses images and _MEMIMAGE to compare, as they're nice and quick to generate, duplicate, and alter.


RE: CompareMem - Dav - 10-13-2023

That can come in handy.  Saving it.  Thank you.

- Dav