KMP匹配算法-效率测试

总结:

  • KMP匹配无规律的匹配字符串效果不太好
  • KMP匹配有关联的字符串效率比较高
  • KMP匹配无关数据,效率也许比简单匹配低

无关数据匹配

//无关数据匹配
//模式匹配字符串P
A   B   C   D   A   B   D   A   B   A   C   D   
-1 0 0 0 -1 0 2 -1 0 2 1 0 //文本串S
E H Z U Z E C A K G  ... I U N V M S N S Z E
------------------ 1024K -------------------- 1048568
0.0100 seconds.
0.0100 seconds. 
------------------end-------------------- ------------------ 10240K -------------------- 10485752
0.0200 seconds.
0.0600 seconds.
------------------end-------------------- ------------------ 102400K -------------------- 104857592
0.2500 seconds.
0.5900 seconds.
------------------end-------------------- ------------------ 1024000K -------------------- 1048575992
//简单算法法
10.1300 seconds.
//KMP算法(时间不包含生成next[])
17.9200 seconds.
------------------end--------------------

有关数据匹配

//有关数据匹配
//模式匹配字符串P
A   B   C   D   A   B   D   A   B   A   C   D   
-1  0   0   0   -1  0   2   -1  0   2   1   0   
//文本串S
C B B D A C C B B A  ... C B C A C D C A B C 
------------------ 1M --------------------
1048564
0.0200 seconds.
0.0100 seconds.
------------------end--------------------
------------------ 10M --------------------
3440052
0.0200 seconds.
0.0300 seconds.
------------------end--------------------
------------------ 100M --------------------
3440052
0.0200 seconds.
0.0300 seconds.
------------------end--------------------
D A C B A A C A D C  ... A C A C C C B C B B 
------------------ 1000M --------------------
46132800
//简单算法法
0.5300 seconds.
//KMP算法(时间不包含生成next[])
0.4400 seconds.
------------------end--------------------
    原文作者:KMP算法
    原文地址: https://blog.csdn.net/cy_cai/article/details/53065713
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞