110,534
社区成员
发帖
与我相关
我的任务
分享
byte[] mBTCmdDataBase = {
0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X06, 0X68, 0X73, 0X5f, 0X62, 0X61, 0X79, 0X00, 0X00, 0X00, 0X00, 0X00, 0X07, 0X00, 0X00, 0X00, 0X20, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X08
};
public int ArrayFindIndexOf(byte [] pBT, byte [] pMatch, int pStartIndex)
{
try
{
int mIndex = -1;
while (mIndex < 0)
{
mIndex = System.Array.IndexOf(pBT, pMatch[0], pStartIndex);
if (mIndex < pStartIndex)
{
return -1;
}
pStartIndex = mIndex + 1;
if (mIndex >= 0)
{
for (int i = 0; i < pMatch.Length; i++)
{
if (pBT[mIndex + i] != pMatch[i])
{
mIndex = -1;
break;
}
}
}
}
return mIndex;
}
catch (NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception Ex)
{
throw Ex;
}
}
using System;
class Program
{
static void Main()
{
int m = 10000000;
int n = 1000;
byte[] s0 = new byte[m];
byte[] s1 = new byte[n];
for (int i = 0; i < m - 1; i++) s0[i] = 1;
for (int i = 0; i < n - 1; i++) s1[i] = 1;
s0[m - 1] = s1[n - 1] = 2;
DateTime time0 = DateTime.Now;
int index = IndexOf(s0, s1);
DateTime time1 = DateTime.Now;
Console.WriteLine("index = {0}, 用时 {1} 秒", index, (time1 - time0).TotalSeconds);
// index = 9999000, 用时 28.703125 秒
}
// 在 s 中查找 pattern 。
// 如果找到,返回 pattern 在 s 中第一次出现的位置(0起始)。
// 如果没找到,返回 -1。
static int IndexOf(byte[] s, byte[] pattern)
{
// ...
}
}