我坚持项目的第二阶段:将一个字节 [] 拆分为 4 个切片(以加载 QuadCore I5 CPU),然后在每个切片上,在每个内核上启动一个线程(比较任务)。
原因是试图加速两个相同大小的字节数组之间的比较我如何处理这个?
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int memcmp(byte[] b1, byte[] b2, long count);
class ArrayView<T> : IEnumerable<T>
{
private readonly T[] array;
private readonly int offset, count;
public ArrayView(T[] array, int offset, int count)
{
this.array = array; this.offset = offset; this.count = count;
}
public int Length { get { return count; } }
public T this[int index] {
get { if (index < 0 || index >= this.count)
throw new IndexOutOfRangeException();
else
return this.array[offset + index]; }
set { if (index < 0 || index >= this.count)
throw new IndexOutOfRangeException();
else
this.array[offset + index] = value; }
}
public IEnumerator<T> GetEnumerator()
{
for (int i = offset; i < offset + count; i++)
yield return array[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
IEnumerator<T> enumerator = this.GetEnumerator();
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
}
public void CopmarArrSlice()
{
byte[] LoadedArr = File.ReadAllBytes("testFileCompare2Scr.bmp");
int LoddArLn = OrgArr.Length;
int range = (LoddArLn / 4) - LoddAremainder;
int divisionremain = LoddArLn - (range * 4);
ArrayView<byte> LddP1 = new ArrayView<byte>(OrgArr, 0, range);
ArrayView<byte> LddP2 = new ArrayView<byte>(OrgArr, p1.Length, range);
ArrayView<byte> LddP3 = new ArrayView<byte>(OrgArr, (p1.Length + p2.Length), range);
ArrayView<byte> LddP4 = new ArrayView<byte>(OrgArr, (p1.Length + p2.Length + p3.Length), range + divisionremain);
if (AreEqual(LddP1, CapturedP1)) ....Do Somthing
}
public bool AreEqual(byte[] a, byte[] b)
{
if (a == b)
return true;
if (a == null || b == null)
return false;
if (a.Length != b.Length)
return false;
return memcmp(a, b, a.Length) == 0;
}
CopmarArrSlice();
在这种情况下,如何使用 AreEqual(using memcmp) 将其与使用 4 个线程/Parallelism 进行比较,以在每个 CpuCore 上进行计算