Hasty Briefsbeta

Bilingual

Span<T>.SequenceEquals is faster than memcmp

a year ago
  • #.NET
  • #Benchmarking
  • #Performance
  • Examining portability issues when migrating from .NET Framework 4.8.1 to .NET 8.
  • Discovery of msvcrt.dll usage for fast byte array comparison in .NET.
  • Introduction of Span<T> in modern .NET for type-safe and memory-safe contiguous memory representation.
  • Performance comparison between memcmp, Span<T>.SequenceEqual, naive loop, and Enumerable.SequenceEqual.
  • Benchmark results showing Span<T>.SequenceEqual outperforming memcmp in .NET 8 and .NET 9.
  • Notable performance regression in .NET 9 for 1GB arrays, requiring further investigation.
  • Recommendation to use IEnumerable<T>.SequenceEqual in .NET 8 for high performance without external dependencies.
  • Advice to use Span<T>.SequenceEqual in .NET Framework for better performance.
  • Suggestion to use order-sensitive hash for frequently compared large arrays to avoid expensive comparisons.
  • Source code available on GitHub for further exploration and contributions.