快速的字符串查找算法(Boyer-Moore)
来源:互联网 发布:网络营销策划文案 编辑:程序博客网 时间:2024/06/07 05:56
来自:http://www.cnblogs.com/unruledboy/archive/2005/07/05/186506.html
在CodeProject,Efficient Boyer-Moore Search in Unicode Strings,作者leseul 展示了Boyer-Moore算法的威力,代码这里下载:
Download source - 10.2 Kb
Download demo project - 5.18 Kb
我写了一个性能测试:
public static void Main()
{
string pattern = "AbC";
string target = "AbCaBc";
string pressure;
StringBuilder b = new StringBuilder();
int count = 10000000;
for (int i = 0; i < count; i++)
{
b.Append(target);
}
pressure = b.ToString();
//BM without case senstive
HiPerfTimer time = new HiPerfTimer();
time.Start();
CIBMSearcher BMS = new CIBMSearcher(pattern, false);
int index = BMS.Search(pressure, 0);
while (index >= 0)
{
index = BMS.Search(pressure, index + pattern.Length);
}
time.Stop();
Console.WriteLine("BM without case senstive:" + time.Duration);
GC.Collect();
//BM with case senstive
time = new HiPerfTimer();
time.Start();
BMS = new CIBMSearcher(pattern, true);
index = BMS.Search(pressure, 0);
while (index >= 0)
{
index = BMS.Search(pressure, index + pattern.Length);
}
time.Stop();
Console.WriteLine("BM with case senstive:" + time.Duration);
GC.Collect();
//SubString without case senstive
time = new HiPerfTimer();
time.Start();
index = pressure.IndexOf(pattern);
while (index >= 0)
{
index = pressure.IndexOf(pattern, index + pattern.Length);
}
time.Stop();
Console.WriteLine("SS without case senstive:" + time.Duration);
GC.Collect();
Console.ReadLine();
}
结果如下:
BM without case senstive:1.2411443536895
BM with case senstive:0.707685620917367
SS without case senstive:1.77157282256596
SS是SubString。
我的电脑是PIV 2.8G + 1GRAM 。
BM的威力可见一斑,估计我之前写的高效的忽略大小写的字符串替换(Replace)函数(多种方法比较)可以大大改善了。
高效的算法的意义就在于此啊!这个算法现暂不研究,今天太忙,得看看今晚是否有时间研究一下。
注释:代码不格式化是因为代码插入功能有错误,无法使用。
点击这里下载我的写的测试代码
Boyer-Moore字符串搜索算法
http://zh.wikipedia.org/wiki/Boyer-Moore字符串搜索算法- 快速的字符串查找算法(Boyer-Moore)
- 快速的字符串查找算法(Boyer-Moore)
- (二)Boyer-Moore字符串查找算法
- 字符串:Boyer-Moore字符串查找算法
- Boyer-Moore算法字符串查找算法
- 查找字符串之boyer-moore算法
- 阮一峰:字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 阮一峰:字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 字符串匹配的Boyer-Moore算法
- 【转载】字符串匹配的Boyer-Moore算法
- Merge Sorted Array
- 用SourceInsight阅读Python工程
- c语言文件中EOF和feof
- 单文档自定义工具栏
- 如何从javascript直接调用word插件
- 快速的字符串查找算法(Boyer-Moore)
- Linux 共享库高级主题
- 制作镜面反射脚本
- MySQL自动测试框架介绍
- 如何配置ASP.NET Web应用程序使之使用Azure SQL Database
- 思科网络实验室路由交换实验指南读书笔记阶段总结1
- ORA-28000: the account is locked-的解决办法
- 实现一个应用程序:从终端接收不确定个数的字符串,并根据这些字符串建立链表
- 第四章 JavaFx 文本处理