Skip to content

Instantly share code, notes, and snippets.

@mgravell
Last active September 25, 2025 09:07
Show Gist options
  • Select an option

  • Save mgravell/8e40e123bad5394f98233c780e95fd0c to your computer and use it in GitHub Desktop.

Select an option

Save mgravell/8e40e123bad5394f98233c780e95fd0c to your computer and use it in GitHub Desktop.
sha1 vs xxhash3
BenchmarkDotNet v0.15.4, Linux Ubuntu 25.04 (Plucky Puffin)
Intel Core Ultra 7 155U 0.40GHz, 1 CPU, 14 logical and 12 physical cores
.NET SDK 9.0.110
[Host] : .NET 9.0.9 (9.0.9, 9.0.925.41916), X64 RyuJIT x86-64-v3
DefaultJob : .NET 9.0.9 (9.0.9, 9.0.925.41916), X64 RyuJIT x86-64-v3
| Method | Length | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
|-------- |--------- |-----------------:|---------------:|-----------------:|------:|--------:|----------:|------------:|
| SHA1 | 1024 | 1,038.23 ns | 20.541 ns | 19.214 ns | 1.00 | 0.03 | - | NA |
| XxHash3 | 1024 | 46.19 ns | 0.953 ns | 1.272 ns | 0.04 | 0.00 | - | NA |
| | | | | | | | | |
| SHA1 | 1048576 | 528,949.80 ns | 10,499.304 ns | 19,720.239 ns | 1.00 | 0.05 | - | NA |
| XxHash3 | 1048576 | 40,954.72 ns | 812.074 ns | 1,027.014 ns | 0.08 | 0.00 | - | NA |
| | | | | | | | | |
| SHA1 | 67108864 | 34,684,811.74 ns | 685,524.563 ns | 1,769,557.932 ns | 1.00 | 0.07 | - | NA |
| XxHash3 | 67108864 | 4,133,490.54 ns | 71,945.439 ns | 56,170.266 ns | 0.12 | 0.01 | - | NA |
using System.IO.Hashing;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
#if DEBUG // just to show working
var obj = new HashBench { Length = 1024 * 1024 };
obj.Setup();
Console.WriteLine(obj.ComputeSha1());
Console.WriteLine(obj.ComputeXxHash3());
#else
BenchmarkRunner.Run<HashBench>();
#endif
[MemoryDiagnoser]
public class HashBench
{
private byte[] _data = [];
private readonly SHA1 _sha1 = SHA1.Create();
[GlobalSetup]
public void Setup()
{
_data = new byte[Length];
Random.Shared.NextBytes(_data);
}
[Params(1024, 1024 * 1024, 64 * 1024 *1024)] public int Length { get; set; }
private static void ThrowNotSupported() => throw new NotSupportedException();
[Benchmark(Description = nameof(SHA1), Baseline = true)]
public int ComputeSha1()
{
Span<byte> target = stackalloc byte[_sha1.HashSize / sizeof(byte)];
if (!_sha1.TryComputeHash(_data, target, out int bytes)) ThrowNotSupported();
return bytes;
}
[Benchmark(Description = nameof(XxHash3))]
public int ComputeXxHash3()
{
_ = XxHash3.HashToUInt64(_data);
return sizeof(ulong);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment