Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查一个字节数组是否包含另一个字节数组的性能方法C#_C#_Arrays_Optimization - Fatal编程技术网

检查一个字节数组是否包含另一个字节数组的性能方法C#

检查一个字节数组是否包含另一个字节数组的性能方法C#,c#,arrays,optimization,C#,Arrays,Optimization,我正在检查一个字节数组是否包含另一个具有以下代码的字节数组: private int IndexOf(int index, byte[] AllBytes, byte[] searchByteArray) { for (int i = index; i <= AllBytes.Length - 1 - searchByteArray.Length - 1; i++) { for (int j = 0; j <= searchByteArray.Leng

我正在检查一个字节数组是否包含另一个具有以下代码的字节数组:

private int IndexOf(int index, byte[] AllBytes, byte[] searchByteArray)
{
    for (int i = index; i <= AllBytes.Length - 1 - searchByteArray.Length - 1; i++)
    {
        for (int j = 0; j <= searchByteArray.Length - 1; j++)
        {
            if (AllBytes[i + j] == searchByteArray[j])
            {
                if (j + 1 == searchByteArray.Length)
                    return i;
            }
            else
                break;
        }
    }
    return -1;
}
private int IndexOf(int索引,byte[]AllBytes,byte[]searchByteArray)
{
对于(inti=index;i您可以调整字节

首先,创建一个数组
int[256]
,其中数组索引对应于所有可能的字节值。该数组包含搜索模式中每个字节值从末尾开始的位置,以及搜索模式中未出现的值的搜索模式长度

然后将模式的最后一个位置与输入数组中的一个位置进行比较。如果值不匹配,则可以通过索引处的表中与输入数组中的值相等的值来提升搜索位置。如果值匹配,则将模式与输入进行比较

范例

intput:         100 206 002 250 123 075 074 109 184 222
search pattern:             200 109 100 150 123
                                             ^
                                             |
桌子

...
[108] = 5
[109] = 3
[110] = 5
...
123
109
不匹配,因此您可以在
109
位置查找表并得到
3
。也就是说,您可以将搜索位置增加
3
,以便两个
109
值对齐。如果值是
108
,则可以移动
5个
位置(完整搜索模式长度)


上面的链接更详细地解释了算法。

这在这里太宽泛了,所以我认为你在这个问题上做得更好。有很多方法。这是一个,有几种不同的算法,有不同的权衡。