博客园 - 灵感之源的Forgio-智能工厂 - 快速的字符串查找算法(Boyer-Moore)

来源:百度文库 编辑:神马文学网 时间:2024/10/04 18:57:29
快速的字符串查找算法(Boyer-Moore)
在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)函数(多种方法比较)可以大大改善了。
高效的算法的意义就在于此啊!这个算法现暂不研究,今天太忙,得看看今晚是否有时间研究一下。
注释:代码不格式化是因为代码插入功能有错误,无法使用。
点击这里下载我的写的测试代码
博客园 - 灵感之源的Forgio-智能工厂 - 快速的字符串查找算法(Boyer-Moore) 快速的字符串查找算法(Boyer-Moore)性能测试代码 博客园 - 灵感之源的Smart Forge智能工厂 - 利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件 学术的四种型态Ernest Boyer 采用部分快速排序算法实现数组的部分排序 - eaglet - 博客园 微软智能之源-WinCE - 天行健的日志 - 网易博客 博客园 - ipointer - RETE算法的描述(原创) 查找字符在字符串中最后出现的位置,急! - Shell - ChinaUnix.net 采用部分快速排序算法实现数组的部分排序 - eaglet的专栏 - CSDN博客 基于英文单词的快速HASH索引算法_小徐博客 学无止境 minix and linux 基于英文单词的快速HASH索引算法_小徐博客 学无止境 minix and linux 字符串hash算法比较|字符串,hash,算法 KMP:在一个长字符串中匹配一个短子串的无回溯算法 KMP:在一个长字符串中匹配一个短子串的无回溯算法 如何准确、快速的查找对方的IP地址 快速、准确查找对方IP地址的方法 用Word的查找替换功能快速给文档分页 - Word之家 - 【软件论坛】_中关村在线圈天... vim内过长字符串导致的语法加亮错误 - 松风 - 博客园 C#连接本地和远程数据库SQL和Access的字符串 - 梦在贝加尔湖 - 博客园 算法连载(2)--快速排序与插入排序的比较 - Compower Studio - CSD... 采用部分快速排序算法实现数组的部分排序 帮你找到一个 单片机开平方的快速算法 字符串的转换 漫谈.NET开发中的字符串编码 - bitfan(数字世界一凡人)的专栏 - CSDN博客