Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- public class Program
- {
- public static void Main()
- {
- Tuple<byte[], byte[], byte[]>[] testdata = _createTestData().ToArray();
- Stopwatch swPinvoke = new Stopwatch();
- Stopwatch swStruct = new Stopwatch();
- Stopwatch swSequenceEqual = new Stopwatch();
- for (int i = 0; i < 1000; i++)
- {
- foreach (Tuple<byte[], byte[], byte[]> testitems in testdata)
- {
- swStruct.Start();
- if (ByteArrayCompare(testitems.Item1, testitems.Item2))
- throw new Exception();
- if (!ByteArrayCompare(testitems.Item1, testitems.Item3))
- throw new Exception();
- swStruct.Stop();
- swPinvoke.Start();
- if (PInvokeCompare(testitems.Item1, testitems.Item2))
- throw new Exception();
- if (!PInvokeCompare(testitems.Item1, testitems.Item3))
- throw new Exception();
- swPinvoke.Stop();
- swSequenceEqual.Start();
- if (testitems.Item1.SequenceEqual((testitems.Item2)))
- throw new Exception();
- if (!testitems.Item1.SequenceEqual((testitems.Item3)))
- throw new Exception();
- swSequenceEqual.Stop();
- }
- }
- Console.WriteLine("PINVOKE: {0}", swPinvoke.Elapsed);
- Console.WriteLine("BITCONVERTER: {0}", swStruct.Elapsed);
- Console.WriteLine("SEQEQUAL: {0}", swSequenceEqual.Elapsed);
- Console.ReadKey();
- }
- private static IEnumerable<Tuple<byte[], byte[], byte[]>> _createTestData()
- {
- for (int i = 1; i < 256; i++)
- {
- byte[] array1 = Enumerable.Range(0, i).Select(i1 => (byte) i1).ToArray();
- byte[] array2 = Enumerable.Range(0, i).Select(i1 => (byte) i1).ToArray();
- byte[] array3 = array1.ToArray();
- // Last element in array is different (worst case)
- array2[array2.Length - 1] -= 1;
- yield return Tuple.Create(array1, array2, array3);
- }
- }
- public static bool ByteArrayCompare(byte[] array1, byte[] array2)
- {
- /* if (array1 == null) throw new ArgumentNullException("array1");
- if (array2 == null) throw new ArgumentNullException("array2");
- if (ReferenceEquals(array1, array2))
- return true;*/
- if (array1.Length != array2.Length)
- return false;
- const int sizeOfUint64 = sizeof (UInt64);
- int length = array1.Length;
- for (int offset = 0; offset < length; offset += sizeOfUint64)
- {
- if (offset + sizeOfUint64 > length)
- {
- for (int j = offset; j < length; j++)
- {
- if (array1[j] != array2[j])
- return false;
- }
- }
- else if (BitConverter.ToUInt64(array1, offset) != BitConverter.ToUInt64(array2, offset))
- return false;
- }
- return true;
- }
- [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
- static extern int memcmp(byte[] b1, byte[] b2, long count);
- public static bool PInvokeCompare(byte[] b1, byte[] b2)
- {
- // Validate buffers are the same length.
- // This also ensures that the count does not exceed the length of either buffer.
- return b1.Length == b2.Length && memcmp(b1, b2, b1.Length) == 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement