gpt4 book ai didi

c# - 在 byte[] (C#) 中搜索 Int32 的最快算法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:28:57 28 4
gpt4 key购买 nike

我想在一个大 (50mb+) 字节数组中搜索一个 int。我应该使用什么算法?也许是一些不安全的方法?

编辑:它不是一个 int 数组,它是一个字节数组。数据未以任何方式排序。

最佳答案

public IList<int> FindIntInBytes(byte[] bytes, int target)
{
IList<int> found = new List<int>();

unsafe
{
fixed (byte* pBytes = bytes)
{
byte* pCurrent = pBytes;
for (int i = 0; i <= bytes.Length - 4; i++, pCurrent++)
{
if (target == *(int*)pCurrent)
{
found.Add(i);
}
}
}
}

return found;
}

不适用于大端架构,但它们不用于大多数 .Net 应用程序。

分成多个部分并在多个线程中运行,然后合并结果以获得更快的性能,具体取决于阵列的大小和 CPU 可用性。

关于c# - 在 byte[] (C#) 中搜索 Int32 的最快算法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7209349/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com