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. Linux常用技巧

    1.解决不能中文显示 xshell 终端语言显示选择UTF-8 #yum groupinstall chinese-support 2.heredocument报错“unexpected end of ...

  2. commons-httpclient和org.apache.httpcomponents的区别

    <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpc ...

  3. 目标检测从入门到精通—SPP-Net详细解析(三)

    SPP-Net网络结构分析 Author:Mr. Sun Date:2019.03.18 Loacation: DaLian university of technology 论文名称:<Spa ...

  4. python使用SUDS调用webservice

    最近做接口对接,遇到了.net开发的webservice接口,因为python第一次与webservice对接,连问带查,最后使用suds库来实现了 1.安装suds mac: sudo pip in ...

  5. ionic view 视图

    ionic view  方法 $ionicView.loaded 视图已经被加载了.这事件只发生一次当视图被创建并添加到Dom中.当跳出页面并且被缓存了的话,再次访问这个页面时这个时间将不会被激活.L ...

  6. Nginx 的常用命令

    nginx命令,先来一波基本操作: start nginx // 启动Nginx nginx -t // 测试配置文件 nginx -v // 查看Nginx版本 nginx -V // 查看Ngin ...

  7. Vue 提示框组件

    OK,首先看看效果: 一.子组件(alert.vue) <template> <transition name="alert"> <div class ...

  8. It\'s A Good Day To Die

    [00:01.82]Courage! Duty! Honor! [00:05.67]We call upon our troopers [00:07.90]In this our darkest ho ...

  9. springboot使用@Aspect实现AOP记录日志讲解

    AOPAOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.在日常开发当中经常用来记录日志,方法跟踪 ...

  10. TZ_01MyBatis_log4j.propertiies

    # Set root category priority to INFO and its only appender to CONSOLE. #log4j.rootCategory=INFO, CON ...