- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我被项目的第二阶段困住了:将一个 byte[] 拆分为 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(使用 memcmp)将其与使用 4 线程/Parallelism 进行比较,以计算每个 CpuCore
最佳答案
我编写了一个尽可能利用多核的函数,但它似乎受到 p/invoke 调用的严重影响。我认为这个版本只有在测试非常大的数组时才有意义。
static unsafe class NativeParallel
{
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int memcmp(byte* b1, byte* b2, int count);
public static bool AreEqual(byte[] a, byte[] b)
{
// The obvious optimizations
if (a == b)
return true;
if (a == null || b == null)
return false;
if (a.Length != b.Length)
return false;
int quarter = a.Length / 4;
int r0 = 0, r1 = 0, r2 = 0, r3 = 0;
Parallel.Invoke(
() => {
fixed (byte* ap = &a[0])
fixed (byte* bp = &b[0])
r0 = memcmp(ap, bp, quarter);
},
() => {
fixed (byte* ap = &a[quarter])
fixed (byte* bp = &b[quarter])
r1 = memcmp(ap, bp, quarter);
},
() => {
fixed (byte* ap = &a[quarter * 2])
fixed (byte* bp = &b[quarter * 2])
r2 = memcmp(ap, bp, quarter);
},
() => {
fixed (byte* ap = &a[quarter * 3])
fixed (byte* bp = &b[quarter * 3])
r3 = memcmp(ap, bp, a.Length - (quarter * 3));
}
);
return r0 + r1 + r2 + r3 == 0;
}
}
在大多数情况下,它实际上比优化的安全版本慢。
static class SafeParallel
{
public static 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;
bool b1 = false;
bool b2 = false;
bool b3 = false;
bool b4 = false;
int quarter = a.Length / 4;
Parallel.Invoke(
() => b1 = AreEqual(a, b, 0, quarter),
() => b2 = AreEqual(a, b, quarter, quarter),
() => b3 = AreEqual(a, b, quarter * 2, quarter),
() => b4 = AreEqual(a, b, quarter * 3, a.Length)
);
return b1 && b2 && b3 && b4;
}
static bool AreEqual(byte[] a, byte[] b, int start, int length)
{
var len = length / 8;
if (len > 0)
{
for (int i = start; i < len; i += 8)
{
if (BitConverter.ToInt64(a, i) != BitConverter.ToInt64(b, i))
return false;
}
}
var remainder = length % 8;
if (remainder > 0)
{
for (int i = length - remainder; i < length; i++)
{
if (a[i] != b[i])
return false;
}
}
return true;
}
}
关于c# - 4 片 bytearray 使用多线程进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252955/
我有以下代码,适用于文本文件,但不适用于 pdf 文件。我的文件包含英语和希腊语字符。我尝试将 pdf 文件转换为 byteStream 并将 byteStream 转换为 String 格式,以便将
我有一些大的二进制文件,我需要在其中搜索特定的字节序列,例如: find_bytes = bytearray(base64.b16decode('a226fb42')) with open(filen
如何将 bytearray 值分配给面板背景图像。如果有人有想法或经验,请帮助我克服这个问题。简要经验: 我有面板控制,想加载从 webservice 获取的图像作为背景图像。所以我使用了 setst
我的 ByteArray 值为 avroBinaryValue ,架构名称值为 String schemaName ,上次修改日期值为 long 的 lastModifiedDate 。 byte[]
我试图在开始时将一个字节数组插入到另一个字节数组中。这是我正在努力完成的一个简单示例。 import struct a = bytearray(struct.pack(">i", 1)) b = by
我正在尝试将 long 和 bytearray 连接到另一个 bytearray。 我尝试过这样的: byte[] value1= new byte[16]; byte[] value2= new b
我有一个任务,我必须在数据库中读取和保存图像。 我正在使用 C#.net Webservice 和 SQlserver2008 数据库,并将图像保存为 varbinary 格式。 我想知道 如何从本地
我在创建 ByteArray var 时遇到了一些问题,其中它的元素也是 ByteArray,我不知道是不是可能先?以及如何? 最佳答案 ByteArray 顾名思义,就是一个字节数组。如果您想保留多
我有三个 bytearray,长度为 40000。我想将字节数组 1 的字节数组索引 0,1、bytearaay2 的索引 0,1 和 bytearray3 的索引合并到 40000 长度。 像这样:
我认为这是一个新手类型的问题,但我很理解这一点。 我可以找到很多关于如何将字符串转换为各种语言的字节数组的帖子。 我不明白的是逐个字符发生了什么。我知道屏幕上显示的每个字符都由一个数字表示,例如它的
锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。 这是我的( Code Golf )挑战: 取两个字节数组并确定第二个数组是否是第一个数组的子字符
我在字节流方面做了很多工作,所以我理解这个概念——有些字节表示比其他字节更高的数值。 在阅读维基上的定义时,我变得非常困惑。 当我看到“Big-endian”或 BIG + END 这个词时,我期望最
我正在使用 Kafka 发送生产和消费消息。 生产很好,使用 制作人。 消费时,我使用下面的代码(取自示例)但我得到的每条记录只有 8 个字节(代码下方的示例输出)。 有没有一种方法可以让消费者简单地
我有很多时间创建缩略图,然后将它们转换为字节数组。我尝试了三种方法,所有 3 次都出现错误。 第一个是使用Bitmap.GetThumbnailImage,我过去用过,然后直接保存到Response.
如何将存储文件分配给 ByteArray? var file = await openPicker.PickSingleFileAsync(); 最佳答案 这里是一个使用文件选择器的示例,获取存储文件
我需要在Kotlin中创建一个包含混合类型的字节数组。 具体来说,我需要编写如下内容: 0, 1, 0x02, 1, "me@emailaddr.com" 使用Kotlin实现BLE的特性。 显然,我
当我运行这个: class SomeTest { fun howToMockByteArray() { val bytes = Mockito.mock(ByteArray::
D中ubyte[]的最大大小有限制吗? 在this线程 我遇到内存不足错误,无法理解是驱动程序问题还是 ubyte[] 大小限制? 最佳答案 D中任意数组的最大长度为size_t.max。但是,由于
如果只有一个写入器将字节附加到一个bytearray,并且多个读取器从中读取数据,那么是否有可能一个或多个读取器读取既不在扩展之前也不在扩展之后的数据? 例如,如果bytearray中的旧数据是012
请耐心听我说;我是系统管理员而不是开发人员。下面的代码对我来说效果很好。但是当我将其分成两个文件以便类位于一个文件中而逻辑位于另一个文件中时,我收到一条错误,指出 data[0] 是 str 并且不支
我是一名优秀的程序员,十分优秀!