Pages

11/18/2014

C# Compare Arrays

 

Sometimes we get so focused on the advanced features of .NET that we (or at least I) forget the basics. The basic in this case is checking to see if two arrays are equal.

int[] a = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };
int[] b = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };
if (b == a) 
  { Console.WriteLine("a Equals b!"); } 
  else 
  { Console.WriteLine("a NOT Equals b!"); }

The above always returns "a NOT Equals b!".  Yes, I knew that if (a == b) (same as a.Equals(b)) will not work as arrays are objects and "a == b" is testing reference equality, i.e. to see if they are the same object. But I also figured that as .NET has a built in class or method for everything you can think of, there had to be one to do an array comparison. Turns out there is, .SequenceEqual., which is part of LINQ.

if (b.SequenceEqual(a))
  { Console.WriteLine("a SequenceEqual b!"); } 
  else 
  { Console.WriteLine("a NOT SequenceEqual b!"); }

The above works great, but as LINQ is wrapper for other code, there must be some overhead, and it turns out there's a lot! I wrote a quick and dirty CompareArrays method and did a comparison of performance between my crude code and the LINQ SequenceEqual. Here's the results:

image

The manual code was around five times faster! BUT, it took a whole lot longer to write. Unless you are writing games or intense code, developer efficiency and code readability leans towards the LINQ SequenceEqual solution.

Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };
            int[] b = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };

            DateTime d1 = DateTime.Now;
            for (int i = 0; i < 100000; i++)
            {
                b.SequenceEqual(a);
            }

            DateTime d2 = DateTime.Now;
            for (int i = 0; i < 100000; i++)
            {
                CompareArrays(a, b);
            }

            DateTime d3 = DateTime.Now;
            Console.Write("Time for SequenceEqual:    ");
            Console.WriteLine(d2 - d1);
            Console.Write("Time for my CompareArrays: ");
            Console.WriteLine(d3 - d2);

            Console.ReadLine();
        }

        static bool CompareArrays(int[] a, int[] b)
        {
            if (a.Length != b.Length) { return false; }
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i]) { return false; }

            }
            return true;

        }

    }
}

 

When I get really bored I convert CompareArrays into generics…

 

Hey! Look! I wrote about something other than SharePoint or PowerShell!

 

.

1 comment:

Anonymous said...

This was really helpful, thank you!

Post a Comment

Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.