快速的字符串查找算法(Boyer-Moore)
来源:互联网 发布:淘宝内购群 编辑:程序博客网 时间:2024/05/22 17:41
在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)
- 快速的字符串查找算法(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算法
- Linux下Socket编程的端口问题( Bind(): Address already in use )
- iOS开发16:使用Navigation Controller切换视图
- Android Theme使用总结
- Linux中的批量操作和文件统计问题
- 文件服务器更换用户登录
- 快速的字符串查找算法(Boyer-Moore)
- JForum源码分析
- .net获取用户电脑名,IP,当前电脑用户
- 1.Strut国际化动态文本(编程式处理异常):界面
- MySQL函数基本用法
- <<软件研发之道>>经验法则4---不要认为别人是笨蛋
- USB安装CentOS失败后,解决办法
- C语言头文件避免重复包含
- redis的快照方式和aof方式简介