Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the fastest way I can compare two equal-size bitmaps to determine whether they are identical?
    primarykey
    data
    text
    <p>I am trying to write a function to determine whether two equal-size bitmaps are identical or not. The function I have right now simply compares a pixel at a time in each bitmap, returning false at the first non-equal pixel.</p> <p>While this works, and works well for small bitmaps, in production I'm going to be using this in a tight loop and on larger images, so I need a better way. Does anyone have any recommendations?</p> <p>The language I'm using is C# by the way - and yes, I am already using the .LockBits method. =)</p> <p><strong>Edit</strong>: I've coded up implementations of some of the suggestions given, and here are the benchmarks. The setup: two identical (worst-case) bitmaps, 100x100 in size, with 10,000 iterations each. Here are the results:</p> <pre><code>CompareByInts (Marc Gravell) : 1107ms CompareByMD5 (Skilldrick) : 4222ms CompareByMask (GrayWizardX) : 949ms </code></pre> <p>In CompareByInts and CompareByMask I'm using pointers to access the memory directly; in the MD5 method I'm using Marshal.Copy to retrieve a byte array and pass that as an argument to MD5.ComputeHash. CompareByMask is only slightly faster, but given the context I think any improvement is useful.</p> <p>Thanks everyone. =)</p> <p><strong>Edit 2</strong>: Forgot to turn optimizations on - doing that gives GrayWizardX's answer even more of a boost:</p> <pre><code>CompareByInts (Marc Gravell) : 944ms CompareByMD5 (Skilldrick) : 4275ms CompareByMask (GrayWizardX) : 630ms CompareByMemCmp (Erik) : 105ms </code></pre> <p>Interesting that the MD5 method didn't improve at all.</p> <p><strong>Edit 3</strong>: Posted my answer (MemCmp) which blew the other methods out of the water. o.O</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload