在BINARY文件(900mb-4.5gb)中搜索byte []并获取偏移量的最快方法。 C#

问题描述 投票:-2回答:1

基本上我想要一种更快,更有效的方法来搜索二进制文件中的字节数组并获取偏移量。 byte []可以包含5-50个字节。将会阅读1条搜索我有一个功能无法正常运行并且非常缓慢:

        static long ReadOneSrch(BinaryReader reader, byte[] bytes)
    {
        int b;
        long i = 0;
        while ((b = reader.BaseStream.ReadByte()) != -1)
        {
            if (b == bytes[i++])
            {
                if (i == bytes.Length)
                    return reader.BaseStream.Position - bytes.Length;
            }
            else
                i = b == bytes[0] ? 1 : 0;
        }

        return -1;
    }
c# offset binaryfiles
1个回答
0
投票

发布的代码看起来基本上可以,但是BinaryReader是不必要的。 EG

using System;
using System.Diagnostics;
using System.IO;

namespace stringsearch
{
    class Program
    {
        static void Main(string[] args)
        {
            var fn = @"C:\Users\david\Downloads\SQLServer2019-x64-ENU-Dev.iso";
            var fi = new FileInfo(fn);

            var search = new byte[] { 12, 23, 17,55 };

            var sw = new Stopwatch();

            sw.Start();

            using var fs2 = new FileStream(fn, FileMode.Open , FileAccess.Read, FileShare.Read, 1024*1024, FileOptions.SequentialScan );

            sw.Restart();
            var pos = ReadOneSrch(fs2, search);
            Console.WriteLine($"{pos} {sw.Elapsed.TotalSeconds}sec");

        }
        static long ReadOneSrch(Stream s, byte[] bytes)
        {
            int b;
            long i = 0;
            long br = 0;
            while ((b = s.ReadByte()) != -1)
            {

                br++;
                if (br % 1000000 == 0) Console.Write(".");
                if (b == bytes[i++])
                {
                    if (i == bytes.Length)
                        return br - bytes.Length;
                }
                else
                {
                   i = b == bytes[0] ? 1 : 0;
                }
            }

            return -1;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.