public unsafe void Crc32CManagedHashIncrementalBenchmark2() { var rng = new Random(42); var len = 15; var mem = BufferPool.Retain(len); var ptr = (byte *)mem.Pointer; rng.NextBytes(mem.GetSpan()); var count = 1_000_000; var sum = 0UL; uint digest = 0; using (Benchmark.Run("CRC32 Managed", count)) { for (int i = 0; i < count; i++) { digest = Crc32C.CalculateCrc32CManaged(ptr, len, digest); } } Benchmark.Dump(); Console.WriteLine(digest); }
public unsafe void CRCBenchmark() { var lens = new int[] { 1, 2, 3, 4, 7, 8, 9, 15, 16, 31, 32, 63, 64, 255, 256, 511, 512, 1023, 1024 }; foreach (var len in lens) { var arr = new byte[len]; var memory = (Memory <byte>)arr; var handle = memory.Pin(); var ptr = (byte *)handle.Pointer; var rng = new Random(42); rng.NextBytes(arr); var count = 1_00_000_000; var cnt = count / len; var sum = 0UL; { uint digest = 0; using (Benchmark.Run("xxHash", count, true)) { for (int i = 0; i < cnt; i++) { digest = XxHash.CalculateHash(ptr, len, digest); } } } uint crc0, crc1, crc2; { uint digest = 0; using (Benchmark.Run("CRC32 Intrinsic", count, true)) { for (int i = 0; i < cnt; i++) { digest = Crc32C.CalculateCrc32C(ptr, len, digest); } } crc0 = digest; } { uint digest = 0; using (Benchmark.Run("CRC32 Managed", count, true)) { for (int i = 0; i < cnt; i++) { digest = Crc32C.CalculateCrc32CManaged(ptr, len, digest); } } crc1 = digest; } { var crc32C = new Ralph.Crc32C.Crc32C(); uint digest = 0; using (Benchmark.Run("CRC32 Ralph", count, true)) { for (int i = 0; i < cnt; i++) { crc32C.Update(arr, 0, arr.Length); digest = crc32C.GetIntValue(); } } crc2 = digest; } Assert.AreEqual(crc0, crc1); Assert.AreEqual(crc0, crc2); Benchmark.Dump("LEN: " + len); } }