模式匹配-BF、KMP、JavaString.indexOf、BM大比拼

闲来无事,比比看。

 

/**

 * 模式匹配-BF、KMP、JavaString.indexOf、BM大比拼

 */

package javay.test;

 

import javay.util.PMBF;

import javay.util.PMBM;

import javay.util.PMKMP;

 

/**

 * 模式匹配-BF、KMP、JavaString.indexOf、BM大比拼

 * @author DBJ

 */

public class TestPM {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

        /* */

        StringBuffer buf = new StringBuffer();

        long start_001 = System.currentTimeMillis();

        for (int i = 0; i < 10000000; i ++) {

            buf.append("abace");

        }

        buf.append("abcde");

        String src = buf.toString();

        long end_001 = System.currentTimeMillis();

        System.out.println("建目串消耗的时间" + (end_001 - start_001) + "毫秒");

 

        /* 模式串 */

        String ptn = "abcde";

        int pos = -1;

 

        /* Brute-Force */

        long start_01 = System.currentTimeMillis();

        pos = PMBF.patternMatch(srcptn);

        long end_01 = System.currentTimeMillis();

        System.out.println("BF 消耗的时间" + (end_01 - start_01) + "毫秒");

        System.out.println(pos);

 

        /* Knuth-Morris-Pratt */

        long start_02 = System.currentTimeMillis();

        pos = PMKMP.patternMatch(src, ptn);

        long end_02 = System.currentTimeMillis();

        System.out.println("KMP消耗的时间" + (end_02 - start_02) + "毫秒");

        System.out.println(pos);

 

        /* String */

        long start_03 = System.currentTimeMillis();

        pos = src.indexOf(ptn);

        long end_03 = System.currentTimeMillis();

        System.out.println("indexOf消耗的时间" + (end_03 - start_03) + "毫秒");

        System.out.println(pos);

 

        /* Boyer-Moore */

        long start_05 = System.currentTimeMillis();

        pos = PMBM.patternMatch(src, ptn);

        long end_05 = System.currentTimeMillis();

        System.out.println("BM 消耗的时间" + (end_05 - start_05) + "毫秒");

        System.out.println(pos);

    }

}

 

 

 

结果:

创建目标串消耗的时间:1248毫秒

 

BF 消耗的时间:1062毫秒

50000000

 

getNext消耗的时间:0毫秒

KMP1消耗的时间:874毫秒

KMP消耗的时间:874毫秒

50000000

 

indexOf消耗的时间:313毫秒

50000000

 

BM 消耗的时间:218毫秒

50000000

 

JavaString不错呀。

 


转载于:https://my.oschina.net/dubenju/blog/420505

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值