gpt4 book ai didi

c# - 在 64 位计算机上扫描两个字节的 C# byte[] 并返回位置的最快方法是什么?

转载 作者:行者123 更新时间:2023-11-30 17:06:56 24 4
gpt4 key购买 nike

我有一个内存中的 byte[] 并且需要找到 1310 所在的偏移量。然后我将使用以下内容来提取该行:

 String oneLine = Encoding.ASCII.GetString(bytes, 0, max);

在 x64 位计算机上搜索这两个字节的最快方法是什么? ..并将其转换为字符串?

除了遍历每个字节、扫描 13 然后扫描 10 之外,我还能做些什么吗?

// Disclaimer: 
// This is just for my curiosity. Perhaps I'll gain a better understanding of
// how .NET interfaces with RAM, the CPU instructions related to comparisons, etc.
//
// I don't suspect a performance problem, but I do suspect a lack of understanding
// (on my part) on how C# does low-level operations.

最佳答案

不确定这是否是“最快的方法”,但您可以查看 Boyer-Moore 算法以找到所需值的索引。

看看这个 SO 线程 Search longest pattern in byte array in C#

Boyer-Moore 会比线性数组遍历更好,因为它可以根据您“针”的长度跳过元素,并且随着“大海捞针”变大它会变得更好。 HTH.

关于c# - 在 64 位计算机上扫描两个字节的 C# byte[] 并返回位置的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14967333/

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