| |

VerySource

 Forgot password?
 Register
Search
View: 1472|Reply: 9

How to compare two byte arrays in .net with the most efficient

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-24 01:30:02
| Show all posts |Read mode
How to compare two byte arrays in .net with the most efficient
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Singapore

Post time: 2020-7-14 11:30:01
| Show all posts
Compare the lengths first. If it is the same, do you compare each element directly? If it is different, return.
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-7-14 14:45:01
| Show all posts
for example;

static bool ByteEquals(byte[] b1, byte[] b2)
        {
            if (b1 == null || b2 == null) return false;
            if (b1.Length != b2.Length) return false;
            for (int i = 0; i <b1.Length; i++)
                if (b1[i] != b2[i])
                    return false;
            return true;
        }
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-7-17 06:45:01
| Show all posts
I remember that there seems to be an instruction in .net that compares the data in two memory areas. I mean that
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-17 19:15:01
| Show all posts
There is basically no algorithm to optimize
However, you can use the following two methods to deal with, and you can get some efficiency improvement.
1. Use stack-based arrays
byte* byteArray= stackalloc byte[10];
Because stack-based arrays do not check for overflow, better performance can be obtained, and because they are used on the stack, there is no memory release problem, and no garbage collection is required.
2. Use pointers
fixed (byte* p1 =&b1[0])
In this way, you can directly use the pointer ++ to process without checking the array boundary. Second, the garbage collection will not move the array
But the above two methods need to be compiled by /Unsafe
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 17:15:01
| Show all posts
public unsafe static bool ByteArraysEqual(byte[] b1, byte[] b2)
        {
            if (b1 == b2) return true;
            if (b1 == null || b2 == null) return false;
            if (b1.Length != b2.Length) return false;
            int len ​​= b1.Length;
            fixed (byte* p1 = b1, p2 = b2)
            {
                int* i1 = (int*)p1;
                int* i2 = (int*)p2;
                while (len >= 4)
                {
                    if (*i1 != *i2) return false;
                    i1++;
                    i2++;
                    len -= 4;
                }
                byte* c1 = (byte*)i1;
                byte* c2 = (byte*)i2;
                while (len> 0)
                {
                    if (*c1 != *c2) return false;
                    c1++;
                    c2++;
                    len--;
                }
            }
            return true;
        }
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 17:30:01
| Show all posts
There are many aspects. I have a similar method to find the longest common substring of a string. You can refer to
[url=http://blog.csdn.net/hackjojo/article/details/6708109][/url]
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 17:45:01
| Show all posts
1. If the host refers to "the least code":
byte[] b1 = new byte[5];
byte[] b2 = new byte[5];
bool bb = b1.Equals(b2);
Boolean value bb is the comparison result

2. If the host refers to "the fastest":
Refer to the 3rd and 6th floors
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 18:00:01
| Show all posts
C#'s four sorting algorithms: bubble sorting (2016-12-29 09:41:31) Reprinted Category: C#
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    /// <summary>
    /// Bubble Sort
    /// </summary>
    public class BubbleSorter
    {
        public void Sort(int[] list)
        {
            int i, j, temp;
            bool done = false;
            j = 1;
            while((j<list.Length)&&(!done))
            {
                done = true;
                for (i=0;i<list.Length-j;i++)
                {
                    if (list[i]>list[i+1])
                    {
                        done = false;
                        temp = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            //Bubble Sort
            BubbleSorter bs=new BubbleSorter();
            bs.Sort(iArrary);
            for(int m=0;m<iArrary.Length;m++)
            Console.Write("{0} ",iArrary[m]);
            Console.WriteLine();
        }
    }
}



C#'s four sorting algorithms: selection sort
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    /// <summary>
    /// select sort
    /// </summary>
    class SelectionSorter
    {
        private int min;
        public void Sort(int [] list)
        {
            for(int i=0;i<list.Length-1;i++)
            {
                min=i;
                for(int j=i+1;j<list.Length;j++)
                {
                    if(list[j]<list[min])
                        min=j;
                }
                int t=list[min];
                list[min]=list[i];
                list[i]=t;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            //Select sort
            //SelectionSorter ss = new SelectionSorter();
            //ss.Sort(iArrary);
            //
            for(int m=0;m<iArrary.Length;m++)
            Console.Write("{0} ",iArrary[m]);
            Console.WriteLine();
        }
    }
}





C#'s four sorting algorithms: insertion sort
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    /// <summary>
    /// Insertion sort
    /// </summary>
    class InsertionSorter
    {
        public void Sort(int [] list)
        {
            for(int i=1;i<list.Length;i++)
            {
                int t=list[i];
                int j=i;
                while((j>0)&&(list[j-1]>t))
                {
                    list[j]=list[j-1];
                    --j;
                }
                list[j]=t;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            //Insertion sort
            InsertionSorter ins = new InsertionSorter();
            ins.Sort(iArrary);
            for(int m=0;m<iArrary.Length;m++)
            Console.Write("{0} ",iArrary[m]);
            Console.WriteLine();
        }
    }
}



The four sorting algorithms of C#: Hill sorting (2016-12-29 10:24:18) Reprinted Category: C#
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    /// <summary>
    /// Hill sorting, Hill sorting is to insert groups into segments.
    /// </summary>
    class ShellSorter
    {
        public void Sort(int [] list)
        {
            int inc;
            for(inc=1;inc<=list.Length/9;inc=3*inc+1);
            for(;inc>0;inc/=3)
            {
                for(int i=inc+1;i<=list.Length;i+=inc)
                {
                    int t=list[i-1];
                    int j=i;
                    while((j>inc)&&(list[j-inc-1]>t))
                    {
                        list[j-1]=list[j-inc-1];
                        j-=inc;
                    }
                    list[j-1]=t;
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SortDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            //Hill sort
            ShellSorter shs = new ShellSorter();
            shs.Sort(iArrary);
            //display
            for(int m=0;m<iArrary.Length;m++)
            Console.Write("{0} ",iArrary[m]);
            Console.WriteLine();
        }
    }
}
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-9-5 18:15:01
| Show all posts
Is the upstairs sorted?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list