title author date CreateTime categories
C# 对 byte 数组进行模式搜索
lindexi
2019-08-31 16:55:58 +0800
2018-07-19 11:16:46 +0800
C#

本文告诉大家几个方法从 byte 数组找到对应的相同序列的数组

最简单的方法是进行数值判断,但是代码最少是使用Linq ,效率比较高是使用 Boyer-Moore 算法,下面就告诉大家几个算法的代码

判断数值

    class ByteArrayRocks
{
public static IEnumerable<int> IndexOf(byte[] source, int start, byte[] pattern)
{
if (IsEmptyLocate(source, start, pattern))
{
yield break;
} for (int i = start; i < source.Length; i++)
{
if (!IsMatch(source, i, pattern))
{
continue;
} yield return i;
}
} private static readonly int[] Empty = new int[0]; private static bool IsMatch(byte[] array, int position, byte[] candidate)
{
if (candidate.Length > (array.Length - position))
{
return false;
} for (int i = 0; i < candidate.Length; i++)
{
if (array[position + i] != candidate[i])
{
return false;
}
} return true;
} private static bool IsEmptyLocate(byte[] array, int start, byte[] candidate)
{
return array == null
|| candidate == null
|| array.Length == 0
|| array.Length < start
|| candidate.Length == 0
|| candidate.Length + start > array.Length;
}
}

这是最简单的方法,参见 https://stackoverflow.com/a/283648/6116637

linq

这个方法的代码最少

    class LinqArraySearch
{
public static IEnumerable<int> IndexOf(byte[] source, int start, byte[] pattern)
{
for (int i = start; i < source.Length; i++)
{
if (source.Skip(i).Take(pattern.Length).SequenceEqual(pattern))
{
yield return i;
}
}
}
}

Boyer-Moore-Horspool 搜索

这是最快的方法

    class BoyerMooreHorspool
{
public static IEnumerable<long> IndexesOf(byte[] source, int start, byte[] pattern)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
} if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
} long valueLength = source.LongLength;
long patternLength = pattern.LongLength; if ((valueLength == 0) || (patternLength == 0) || (patternLength > valueLength))
{
yield break;
} var badCharacters = new long[256]; for (var i = 0; i < 256; i++)
{
badCharacters[i] = patternLength;
} var lastPatternByte = patternLength - 1; for (long i = 0; i < lastPatternByte; i++)
{
badCharacters[pattern[i]] = lastPatternByte - i;
} long index = start; while (index <= valueLength - patternLength)
{
for (var i = lastPatternByte; source[index + i] == pattern[i]; i--)
{
if (i == 0)
{
yield return index;
break;
}
} index += badCharacters[source[index + lastPatternByte]];
}
}
}

参见:https://stackoverflow.com/q/16252518/6116637

测试了三个方法的性能,请看下面

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.1.202
[Host] : .NET Core 2.0.9 (CoreCLR 4.6.26614.01, CoreFX 4.6.26614.01), 64bit RyuJIT
DefaultJob : .NET Core 2.0.9 (CoreCLR 4.6.26614.01, CoreFX 4.6.26614.01), 64bit RyuJIT
Method Mean Error StdDev
Linq 13,332.8 us 251.782 us 466.694 us
ByteArrayRocks 371.3 us 7.327 us 14.462 us
BoyerMooreHorspool 108.3 us 1.153 us 1.079 us

其他方法请看下面

使用不安全代码的 Boyer Moore 算法 C# High Performance Boyer Moore Byte Array Search Algorithm

2019-8-31-C#-对-byte-数组进行模式搜索的更多相关文章

  1. C# 对 byte 数组进行模式搜索

    本文告诉大家几个方法从 byte 数组找到对应的相同序列的数组 最简单的方法是进行数值判断,但是代码最少是使用Linq ,效率比较高是使用 Boyer-Moore 算法,下面就告诉大家几个算法的代码 ...

  2. 图片和byte[]数组互转

    一.图片转成byte[]数组. import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io ...

  3. 你的环境有问题吧?--byte数组转字符串的疑惑

    1. 故事背景 小T是个测试MM,小C是个程序猿,今天早上他们又为一个bug吵架了. 小T:“这个显示是bug,在我的浏览器上显示不正确” 小C:“这个bug我不认,在我的电脑上显示正常,是你的环境有 ...

  4. 将几张图片合并为一张图片,返回byte数组

    需求:通过url数组下载图片,再竖直合成一张新的图片,具体java代码如下 1 /** 2 * 竖直合并图片 3 * 4 * @param urls 5 * @return 6 */ 7 public ...

  5. 使用buffered流结合byte数组,读入文件中的内容,包括中文字符

    package com.itcast.demo05.Buffered;import java.io.BufferedInputStream;import java.io.FileInputStream ...

  6. go语言:多个[]byte数组合并成一个[]byte

    场景:在开发中,要将多个[]byte数组合并成一个[]byte,初步实现思路如下: 1.获取多个[]byte长度 2.构造一个二维码数组 3.循环将[]byte拷贝到二维数组中 package gst ...

  7. byte数组和File,InputStream互转

    1.将File.FileInputStream 转换为byte数组: File file = new File("file.txt"); InputStream input = n ...

  8. C# byte数组与Image的相互转换

    功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ...

  9. 透过byte数组简单分析Java序列化、Kryo、ProtoBuf序列化

    序列化在高性能网络编程.分布式系统开发中是举足轻重的之前有用过Java序列化.ProtocolBuffer等,在这篇文章这里中简单分析序列化后的byte数组观察各种序列化的差异与性能,这里主要分析Ja ...

  10. 字符串与byte数组转换

    string weclome=""; byte[] data = new byte[1024]; //字符串转byte数组 data = Encoding.ASCII.GetByt ...

随机推荐

  1. dd- Linux必学的60个命令

    1.作用 dd命令用来复制文件,并根据参数将数据转换和格式化. 2.格式 dd [options] 3.[opitions]主要参数 bs=字节:强迫 ibs=<字节>及obs=<字 ...

  2. 已发布的jsp项目如何在本地展示

    已发布的项目可以放到tomcat 安装目录下的webapps/下面,但是有时候我们的目录已经放好了,所在要加映射过去. 1, 到tomcat/conf/下,打开server.xml 2, 找到 < ...

  3. spring_配置处理器对象、处理器映射器、处理器适配器、视图解析器

    创建spring配置文件:application-context.xml. 创建处理器类 package com.lanou.demo.controller;public class BookCont ...

  4. phpqrcode.php 生成二维码图片用于推广

    <?php /* * PHP QR Code encoder * * This file contains MERGED version of PHP QR Code library. * It ...

  5. 469 Same Tree

    原题网址:https://www.lintcode.com/problem/same-tree/description 描述 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构, ...

  6. NSIS使用WinVer.nsh头文件判断操作系统版本

    NSIS使用WinVer.nsh头文件判断操作系统版本,首先请下载最新的WinVer.nsh: http://nsis.sourceforge.net/Include/WinVer.nsh(下载后置于 ...

  7. idea2018.1.5永久破解过程

    可以根据官网推荐注册idea:http://idea.lanyus.com/ 步骤如下:1 下载破解(crack) jar 包 链接:https://pan.baidu.com/s/1-COPHVJi ...

  8. loj6402 校门外的树(dp,多项式求逆)

    https://loj.ac/problem/6402 庆祝一下,,,第一个我自己做出来的,,,多项式的题(没办法,我太弱 虽然用了2个小时才想出来,但这毕竟是0的突破…… 首先声明,虽然我写的题解很 ...

  9. textarea高度自动增高

    <!--随着textarea 输入内容 自动增加高度--> <script type="text/javascript"> $(".input_t ...

  10. Hdu 4965(矩阵快速幂)

    题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...