Solved

Simple bytearray comparing question.

Posted on 2003-12-02
6
242 Views
Last Modified: 2010-04-16
byte[] a = new byte[2];
byte[] b = new byte[2];
a[0] = 1;
a[1] = 2;
b[0] = 1;
b[1] = 2;
Console.WriteLine( a == b );
a[0] = 1;
a[1] = 2;
b[0] = 2;
b[1] = 2;
Console.WriteLine( a == b);

How can i effeciently compare if the values in a is equal to b ( in my base, the size of array should be more then 1M) ? I want the result to be true false, instead of false false that i get now.
0
Comment
Question by:ho_alan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Assisted Solution

by:testn
testn earned 35 total points
ID: 9863650
If you use == on reference type, it will compare the pointer instead of the value inside. That's why you got false in both cases
0
 
LVL 18

Expert Comment

by:testn
ID: 9863661
This means that to compare them, you have to compare it byte by byte....

    public bool CompareArrays (byte[] first, byte[] second)
    {
        if (first.Length != second.Length)
            return false;
        for (int i=0; i < first.Length; i++)
            if (first[i] != second[i])
                return false;
        return true;
    }
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 65 total points
ID: 9865080
As you will compare huge arrays, consider the following optimization of the code. Note that you must enable unsafe blocks in the project properties.

unsafe public bool CompareArrays (byte[] first, byte[] second)
{
      if (first.Length != second.Length)
            return false;

      fixed (byte* f = first)
            fixed (byte *s = second)
            {
                  long* p1 = (long*)f;
                  long* p2 = (long*)s;

                  // Compare as much as possible as longs
                  for (int i = 0; i < first.Length / sizeof (long); i++)
                        if (p1[i] != p2[i])
                              return false;

                  // Compare the rest as bytes
                  for (int i = 0; i < first.Length % sizeof (long); i++)
                        if (f[(first.Length / sizeof (long)) * sizeof (long) + i] !=
                              s[(first.Length / sizeof (long)) * sizeof (long) + i])
                              return false;

                  return true;
            }
}

An example usage:
private void button1_Click(object sender, System.EventArgs e)
{
      byte[] a = new byte[10000];
      byte[] b = new byte[10000];

      Random r = new Random();

      for (int i = 0; i < a.Length; i++)
            a[i] = b[i] = (byte)r.Next();

      MessageBox.Show (CompareArrays(a, b).ToString());
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:ho_alan
ID: 9865190
Hi TheAvenger

In my understanding, you are trying to convert the byte array into a long array first, then compare each block of the long array 1 by 1. So.. the looping will be 4 times less? So.. it still need to loop..

Is there a method which is similar to the function memcmp in c++?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9865209
Actually it's 8 times faster, because the long type in C# is 8 bytes, byte is 1 byte.

I don't know about such a method, but this does not mean it does not exist....
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9865212
By the way this loop compares 10 Mb arrays for ~0.5 sec so I think it has a good performance...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question