没有绑定检查的C#byte []比较

我正在寻找性能有效的方法来比较两个字节[]的相等性.大小超过1 MB,因此应尽量减少每个数组元素的开销.

对于两个阵列,我的目标是在avoiding the repetitive bound checks之前击败SequenceEqualhand-coded for-loop over every item的速度.就像Array.copy可以导致快速memcpy一样,什么会导致memcmp?

解决方法

如果性能真的很重要,那么最快的方法是使用每个Windows版本附带的CRT库.这个代码在我的poky笔记本电脑上需要大约51毫秒,也适用于64位机器:
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

class Program {
  static void Main(string[] args) {
    byte[] arr1 = new byte[50 * 1024 * 1024];
    byte[] arr2 = new byte[50 * 1024 * 1024];
    var sw = Stopwatch.StartNew();
    bool equal = memcmp(arr1,arr2,arr1.Length) == 0;
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
    Console.ReadLine();
  }
  [DllImport("msvcrt.dll")]
  private static extern int memcmp(byte[] arr1,byte[] arr2,int cnt);
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...