什么是我可以比较两个大小相等的位图,以确定它们是否是相同的最快方法? [英] What is the fastest way I can compare two equal-size bitmaps to determine whether they are identical?

查看:191
本文介绍了什么是我可以比较两个大小相等的位图,以确定它们是否是相同的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个函数来确定两个大小相等的位图是否相同与否。我现在这个程序只是在每个位时间的像素相比,在第一个不相等的像素返回false。

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.

虽然这个作品,并非常适用于小型的位图,在生产中我要在紧密循环和较大的图像将使用这一点,所以我需要一个更好的办法。有没有人有什么建议?

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?

我使用的语言是C#的方式 - 是的,我已经使用了.LockBits方法。 =)

The language I'm using is C# by the way - and yes, I am already using the .LockBits method. =)

修改:我codeD了一些给出的建议实现,并且这里的基准。设置:两个相同的(最差情况)的位图,100×100的大小,与各以一迭代。下面是结果:

Edit: 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:

CompareByInts (Marc Gravell) :   1107ms
CompareByMD5  (Skilldrick)   :   4222ms
CompareByMask (GrayWizardX)  :    949ms

在CompareByInts和CompareByMask我用指针直接访问内存;在MD5方法我用Marshal.Copy检索字节数组并传递作为参数传递给MD5.ComputeHash。 CompareByMask只是稍快,但鉴于我认为,任何改善是有用的情况下。

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.

谢谢大家。 =)

编辑2 :忘记打开优化上 - 这样做,让GrayWizardX的回答更是一个提升的:

Edit 2: Forgot to turn optimizations on - doing that gives GrayWizardX's answer even more of a boost:

CompareByInts   (Marc Gravell) :    944ms
CompareByMD5    (Skilldrick)   :   4275ms
CompareByMask   (GrayWizardX)  :    630ms
CompareByMemCmp (Erik)         :    105ms

有趣的是MD5方法根本没有改善。

Interesting that the MD5 method didn't improve at all.

修改3 :发布我的回答(MemCmp),它吹响了其他方法出来的水。 o.O

Edit 3: Posted my answer (MemCmp) which blew the other methods out of the water. o.O

推荐答案

编辑12年8月31日:每乔伊的下面发表评论,留心你比较位图的格式。它们可能包含上呈现位图的不平等大踏步填充,尽管等同逐像素。请参见这个问题了解更多详情。

Edit 8-31-12: per Joey's comment below, be mindful of the format of the bitmaps you compare. They may contain padding on the strides that render the bitmaps unequal, despite being equivalent pixel-wise. See this question for more details.

阅读<一href=\"http://stackoverflow.com/questions/43289/comparing-two-byte-arrays-in-net/1445405#1445405\">this回答以比较有关字节数组取得了更快的方法的问题:使用P / Invoke和MSVCRT的memcmp API调用。这里的code:

Reading this answer to a question regarding comparing byte arrays has yielded a MUCH FASTER method: using P/Invoke and the memcmp API call in msvcrt. Here's the code:

[DllImport("msvcrt.dll")]
private static extern int memcmp(IntPtr b1, IntPtr b2, long count);

public static bool CompareMemCmp(Bitmap b1, Bitmap b2)
{
    if ((b1 == null) != (b2 == null)) return false;
    if (b1.Size != b2.Size) return false;

    var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    var bd2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

    try
    {
        IntPtr bd1scan0 = bd1.Scan0;
        IntPtr bd2scan0 = bd2.Scan0;

        int stride = bd1.Stride;
        int len = stride * b1.Height;

        return memcmp(bd1scan0, bd2scan0, len) == 0;
    }
    finally
    {
        b1.UnlockBits(bd1);
        b2.UnlockBits(bd2);
    }
}

这篇关于什么是我可以比较两个大小相等的位图,以确定它们是否是相同的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆