字符串匹配算法比较

做了一个很粗糙的实验,比较了几种字符串匹配算法的性能。程序用-O3进行编译优化。以下为待查找的文本长度为434018字节,模式串长度为4时 的典型实验结果。可以看到,horspool算法最快,表现最差的为KMP系的shift_and算法(实验结果与《柔性字符串匹配》一书中的结果一 致)。

strstr(C库函数) time:743 微秒
horspool:   time:642 微秒
shift_and:   time:1465 微秒
DNDM:   time:721 微秒

以下为horspool,shift_and和DNDM算法的实验源码:

// horspool算法:计算模式串pat在文本txt中出现的次数

int horspool(const char *txt,const char *pat)
{
    short d[256];
    short m = strlen(pat); /**< m is the length of pat */
    // preprocessing
    for(unsigned short c = 0; c < 256; c++)
        d[c] = m;
    for(short i = 0; i < m-1; i++){
        d[(unsigned char)pat[i]] = m - i - 1;
    }      
    // searching
    const char *p = txt;          /**< current pointer */
    const char *t = txt + strlen(txt) - m;
    int cnt = 0;          /**< the exist times of pat in txt */
    int jj = m-1;
    while(p <= t){
        int j = jj;
        while(j >= 0 && pat[j] == p[j])
            j--;   
        if(j == -1)
            cnt++;
        p += d[(unsigned char)p[m-1]];
    }
    return cnt;
}

// Shift_And算法:计算模式串pat在文本txt中出现的次数

int shift_and(const char *txt, const char *pat)
{
    long b[256];
    int m = strlen(pat);
    for(int i = 0; i < 256; i++)
        b[i] = 0;
    for(int i = 0; i < m; i++)
        b[(unsigned char)pat[i]] |= (0x1 << i);
    int cnt = 0;
    long d = 0;
    const char *s = txt;
    const char *end = txt + strlen(txt);
    long mask = 0x1<<m-1;
    while(s < end){
        d = ((d<<1) | 0x1) & b[(unsigned char)*s];
        if(d & mask)
            cnt ++;
        s++;
    }
    return cnt;
}

// BNDM算法:计算模式串pat在文本txt中出现的次数

int BNDM(const char *txt, const char *pat)
{
    long b[256];
    int m = strlen(pat);
    for(int i = 0; i < 256; i++)
        b[i] = 0;
    for(int i = 0; i < m; i++)
        b[(unsigned char)pat[i]] |= (0x1 << (m-i-1));
    const char *limit = txt + strlen(txt) - m;
    const char *s = txt;
    int cnt = 0;
    long mask = 0x1 << (m-1);
    while(s <= limit){
        int j = m-1;
        int last = m-1;
        long d = -1;
        while(d != 0){
            d &= b[(unsigned char)s[j]];
            j--;
            if(d & mask){
                if(j >= 0)
                    last = j;
                else
                    cnt++;
            }
            d <<= 1;
        }
        s += last+1;
    }
    return cnt;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值