主要用到两个指令集,CompareEqual指令与MoveMask指令,因为电脑cpu原因,我们采用Avx2。
Avx2.CompareEqual,比较两个Vector256<byte>向量,如果元素相同返回255,否则返回0。
Avx2.MoveMask如果Vector256<byte>向量全为为非0且所有元素相等,返回-1,全为0返回0,其它返回其它整数。
代码如下:
但是如果文件太大,一次性加载的数据太多了也慢,而且占用内存,我们可以分批次加载,如下:
static unsafe bool CompareFileContent2(string file1, string file2)
{
const int size = 10;
int vsize = Vector256<byte>.Count;
int count = vsize << size;
using FileStream fs1 = File.Open(file1, FileMode.Open);
using FileStream fs2 = File.Open(file2, FileMode.Open);
byte* one = stackalloc byte[count];
byte* two = stackalloc byte[count];
Vector256<byte> vectorOne;
Vector256<byte> vectorTwo;
var oneSpan = new Span<byte>(one, count);
var twoSpan = new Span<byte>(two, count);
bool result = true;
int len1, len2;
while (true)
{
len1 = fs1.Read(oneSpan);
len2 = fs2.Read(twoSpan);
if (len1 != len2)
{
result = false;
break;
}
if(len1 == 0 && len2==0)
break;
if(len1 == 0 || len2 == 0)
{
result = false;
break;
}
for (int i = 0; i < count; i+= vsize)
{
vectorOne = *(Vector256<byte>*)(one+i);
vectorTwo = *(Vector256<byte>*)(two+i);
var equalMask = Avx2.CompareEqual(vectorOne, vectorTwo);
if (Avx2.MoveMask(equalMask) != -1)
{
result = false;
return result;
}
}
}
return result;
}