Search code examples
c#.netperformancehashchecksum

Is there any very rapid checksum generation algorithm?


Searched around a bit, but I didn't really find what I wat looking for.

I have to validate about 100 byte[16384]'s every second (+ many other tasks..). The biggest problem that looks around the corner is speed.

Do you guys know any good checksum algorithm within C#.NET that is insanely fast? It does not have to be very exact, but if a single bit changes, the checksum should (usually..) change as well.

The byte's are stored in memory, so there's no IO stuff which slows it down.

Thanks!


Solution

  • If each single bit matters, the checksum algorithm would have to process each and every byte. A simple algorithm is simply adding each value and ignoring overflow:

        static unsafe uint GetChecksum(byte[] array)
        {
            unchecked
            {
                uint checksum = 0;
                fixed (byte* arrayBase = array)
                {
                    byte* arrayPointer = arrayBase;
                    for (int i = array.Length - 1; i >= 0; i--)
                    {
                        checksum += *arrayPointer;
                        arrayPointer++;
                    }
                }
                return checksum;
            }
        }
    

    Of course you may not detect all changes and get duplicates, but it may give you an indication on how a fast algorithm performs.