BinaryCompare() - improvements welcome

Post your working scripts, libraries and tools
Posts: 12
Joined: 11 Sep 2016, 04:02

BinaryCompare() - improvements welcome

17 Aug 2018, 01:30

I have a simple function that makes binary comparisons using only native ahk commands. It works reliably, but is about 60% the speed of fc. If anyone has any thoughts about how to further speed up the code, I'm all ears.

ReadUInt64 is used over ReadUChar because of a very large speed increase, presumably from fewer disk reads.

The return codes are the similar to fc.
-1: There was an unidentified error in the comparison process
0: files are identical
1: data mismatch
2a: couldn't find the first file
2b: couldn't find the second file
3: size mismatch; useful when copying across disks.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Posts: 4991
Joined: 19 Dec 2016, 01:58
Location: UK

Re: BinaryCompare() - improvements welcome

17 Aug 2018, 02:21

- Great script.
- In this script, used for creating file hashes, jNizM reads in 0x40000 bytes at a time, instead of 8 bytes at a time, by using f.RawRead(data, 262144).
AHK_CNG/bcrypt_sha1_file.ahk at master · jNizM/AHK_CNG · GitHub
- You can use this code to check if two sets of binary data match.
ret := DllCall("msvcrt\memcmp", Ptr,&data1, Ptr,&data2, UPtr,size, "Cdecl")
- Also, I would use parentheses here (in some situations, without parentheses, AHK uses legacy handling of if statements):
if (A_LastError = 0)
Although maybe this is needed instead, I can't remember.

Code: [Select all] [Download] GeSHi © Codebox Plus

if !F1 := FileOpen(File1,"r")
return "2a" ; couldn't find the first file
- And I think there is one unnecessary break line.
- You can bunch up like this, but it might look neater as you've done it.
F1.Close(), F2.Close()
- Also, F1.Seek(0), F2.Seek(0) should be used, as file reading skips 2 bytes if UTF-16 or 3 bytes if UTF-8, because of BOMs.
- Also, you could add a check, if both files are 0 bytes then they match.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 24 guests