Re: Binary comparison of files
Dave Jones <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.advanced |
|---|---|
| Message-ID | <1598001925-1233173914-cardhu_decombobulator_blackberry.rim.net-1288214494-@bxe1015.bisx.produk.on.blackberry> |
Just to throw this in the mix because nobody else has. You could attach a filesystemwatcher to the directory and pick up changes once the file has changed. Dave Dave JONES [email protected] 06.50.14.38.33 -----Original Message----- From: Eddie Lascu <[email protected]> Date: Wed, 28 Jan 2009 15:13:31 To: <[email protected]> Subject: Re: [ADVANCED-DOTNET] Binary comparison of files Kim, I am sure you have noticed that Geoff's code also compares the lengths of the two files and only if they match proceeds to compare them byte by byte. It's true, it does it after reading the content of the two files and I believe this is one aspect you thought your solution may be more efficient. Regards, Eddie -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[email protected]] On Behalf Of Kim Major Sent: Wednesday, January 28, 2009 3:03 PM To: [email protected] Subject: Re: [ADVANCED-DOTNET] Binary comparison of files The code below also reads the whole file " byte[] referenceFile = File.ReadAllBytes (referenceFilename);" You would probably be better off by comparing 1) the size and 2) then two file streams byte by byte. Like this: http://jopinblog.wordpress.com/2008/05/06/compare-files-method-in-c-unit-tes ting/ Kim Major Renaissance Computer Systems Ltd. Blog: http://blogs.microsoft.co.il/blogs/kim http://www.renaissance.co.il -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[email protected]] On Behalf Of Eddie Lascu Sent: Wednesday, January 28, 2009 9:53 PM To: [email protected] Subject: Re: [ADVANCED-DOTNET] Binary comparison of files Geoff, Are you working on the same project as me? It's like you are doing exactly what I need. I agree 100% with your comments. Hashing is not the best approach because it will require the processing of the whole file. Thanks bunches, Eddie -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[email protected]] On Behalf Of Geoff Taylor Sent: Wednesday, January 28, 2009 1:27 PM To: [email protected] Subject: Re: [ADVANCED-DOTNET] Binary comparison of files > If all you need to know is that the files are different; I would use a > hash. CRC32 is fairly fast. For more reliability you can use a > cryptographic hash. > > If you want to know what the differences are; the performance would > depend on how you plan on processing the differences. Surely a hash will require processing all the file before returning its result? So if the first byte is different, it's still going to read in all of both files and calculate both hashes. Why not just go for a simpler approach using buffered IO. You read two chunks (one chunk from each file) into byte buffers then do a byte-by-byte comparison, breaking the loop at the first byte that doesn't match. Since they're small files, you could probably just do a ReadAllBytes instead of reading into buffers. That would give you the length of both, so you could check that first, giving you another shortcut to a fast negative. Here's a similar method that uses assertions instead of failure conditions that should show you what I mean: public static void Compare (string referenceFilename, string testFilename) { byte[] referenceFile = File.ReadAllBytes (referenceFilename); byte[] testFile = File.ReadAllBytes (testFilename); Assert.AreEqual (testFile.Length, referenceFile.Length, "Files are of different lengths. Reference file is {0} bytes, test file is {1} bytes.", referenceFile.Length, testFile.Length); for (int counter = 0; counter < referenceFile.Length; counter++) { if (referenceFile [counter] != testFile [counter]) { Assert.Fail ("Files do not match (at position " + counter + " - [before '" + indicatorString + "'])."); } } return; } I'm pretty sure this'll be faster than using a hash (although I'd love to see a comparison of timings). You might be able to make the method faster by looking at how the data is read in. Good luck, Geoff =================================== View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives =================================== View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives =================================== View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives =================================== View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives