如何优化此类?
使其更快。这个研究人员类在文件上搜索字节偏移量,然后在用字节替换此偏移量之后。 目前,它需要30秒,而我希望它需要4-5秒。
我的课程如下:
public static class Researcher
{
public static List<long> FindPosition(Stream stream, int searchPosition, long startIndex, byte[] searchPattern)
{
List<long> searchResults = new List<long>();
stream.Position = startIndex;
while (true)
{
if (stream.Position == 5000000000)
return searchResults;
var latestbyte = stream.ReadByte();
if (latestbyte == -1) break;
if (latestbyte == searchPattern[searchPosition])
{
searchPosition++;
if (searchPosition == searchPattern.Length)
{
searchResults.Add(stream.Position - searchPattern.Length);
return searchResults;
}
}
else
{
searchPosition = 0;
}
}
return searchResults;
}
}
当我使用课程时:
foreach (long s in Researcher.FindPosition(fs, 0, 0, bytes searched))
{
fs.Close();
BinaryWriter binaryWriter = new BinaryWriter((Stream)File.Open(filePath, FileMode.Open, FileAccess.ReadWrite));
binaryWriter.BaseStream.Seek(s + 0L, SeekOrigin.Begin);
binaryWriter.Write(bytes wich replace);
binaryWriter.Close();
}