【发布时间】:2012-04-02 05:27:47
【问题描述】:
几周前,我向 Stackoverflow 提出了一个关于创建一种高效算法来搜索大量文本中的模式的问题。现在我正在使用字符串函数 indexOf 进行搜索。一个建议是使用 Rabin-Karp 作为替代方案。我写了一个如下的小测试程序来测试 Rabin-Karp 的实现如下。
public static void main(String[] args) {
String test = "Mary had a little lamb whose fleece was white as snow";
String p = "was";
long start = Calendar.getInstance().getTimeInMillis();
for (int x = 0; x < 200000; x++)
test.indexOf(p);
long end = Calendar.getInstance().getTimeInMillis();
end = end -start;
System.out.println("Standard Java Time->"+end);
RabinKarp searcher = new RabinKarp("was");
start = Calendar.getInstance().getTimeInMillis();
for (int x = 0; x < 200000; x++)
searcher.search(test);
end = Calendar.getInstance().getTimeInMillis();
end = end -start;
System.out.println("Rabin Karp time->"+end);
}
这是我正在使用的 Rabin-Karp 的实现:
import java.math.BigInteger;
import java.util.Random;
public class RabinKarp {
private String pat; // the pattern // needed only for Las Vegas
private long patHash; // pattern hash value
private int M; // pattern length
private long Q; // a large prime, small enough to avoid long overflow
private int R; // radix
private long RM; // R^(M-1) % Q
static private long dochash = -1L;
public RabinKarp(int R, char[] pattern) {
throw new RuntimeException("Operation not supported yet");
}
public RabinKarp(String pat) {
this.pat = pat; // save pattern (needed only for Las Vegas)
R = 256;
M = pat.length();
Q = longRandomPrime();
// precompute R^(M-1) % Q for use in removing leading digit
RM = 1;
for (int i = 1; i <= M - 1; i++)
RM = (R * RM) % Q;
patHash = hash(pat, M);
}
// Compute hash for key[0..M-1].
private long hash(String key, int M) {
long h = 0;
for (int j = 0; j < M; j++)
h = (R * h + key.charAt(j)) % Q;
return h;
}
// Las Vegas version: does pat[] match txt[i..i-M+1] ?
private boolean check(String txt, int i) {
for (int j = 0; j < M; j++)
if (pat.charAt(j) != txt.charAt(i + j))
return false;
return true;
}
// check for exact match
public int search(String txt) {
int N = txt.length();
if (N < M)
return -1;
long txtHash;
if (dochash == -1L) {
txtHash = hash(txt, M);
dochash = txtHash;
} else
txtHash = dochash;
// check for match at offset 0
if ((patHash == txtHash) && check(txt, 0))
return 0;
// check for hash match; if hash match, check for exact match
for (int i = M; i < N; i++) {
// Remove leading digit, add trailing digit, check for match.
txtHash = (txtHash + Q - RM * txt.charAt(i - M) % Q) % Q;
txtHash = (txtHash * R + txt.charAt(i)) % Q;
// match
int offset = i - M + 1;
if ((patHash == txtHash) && check(txt, offset))
return offset;
}
// no match
return -1; // was N
}
// a random 31-bit prime
private static long longRandomPrime() {
BigInteger prime = new BigInteger(31, new Random());
return prime.longValue();
}
// test client
}
Rabin-Karp 的实现可以返回我要查找的字符串的正确偏移量。令我惊讶的是,当我运行测试程序时发生的时间统计。他们在这里:
Standard Java Time->39
Rabin Karp time->409
这真是令人惊讶。 Rabin-Karp(至少在这里实现)不仅不比标准的 java indexOf String 函数快,而且慢了一个数量级。我不知道出了什么问题(如果有的话)。有人对此有想法吗?
谢谢,
艾略特
【问题讨论】:
-
顺便说一句,
BigInteger(31, new Random())不返回素数(至少有时) -
首先,您正在搜索一个非常短的字符串...
-
我没有仔细查看代码,也不知道 Rabin-Karb 算法,但
in a large chunk of text暗示该算法针对大文本进行了优化,对于较小的文本可能会更慢。您是否也尝试过使用更大的文本? -
你试过用一个非常大的干草堆吗?它的性能优势可能只会在更大的范围内体现出来。
-
@MartínCanaval 据我所知(查看 Java 6 源代码)
indexOf不是本机实现的。它只是普通的旧 Java。
标签: java string algorithm search rabin-karp