为什么JDK中String类的indexof不使用KMP或者Boyer-Moore等时间复杂度低的算法
2015-05-16 00:00
183 查看
摘要: 为什么JDK中String类的indexof不使用KMP或者Boyer-Moore等时间复杂度低的算法,而是使用原始实现
今天在leetcode上刷题,正好刷到查找字符串的题目,想到了以前了解的KMP和Boyer-Moore等算法。这两个及其类似的算法的时间复杂度都接近于O(n)。
后面自己又去看了下JDK的String类中的indexof方法的实现,发现很奇怪,仅仅只是用了暴力破解法,也就是最原始的实现,时间复杂度也到了O(n*m)。
String类的indexof(String s)方法中调用一下方法:
谷歌并翻了下StackOverflow:
原来JDK的编写者们认为大多数情况下,字符串都不长,使用原始实现可能代价更低。因为KMP和Boyer-Moore算法都需要预先计算处理来获得辅助数组,需要一定的时间和空间,这可能在短字符串查找中相比较原始实现耗费更大的代价。而且一般大字符串查找时,程序员们也会使用其它特定的数据结构,查找起来更简单。这有点类似于排除特定情况下的快速排序了。不同环境选择不同算法。
Reference:
http://stackoverflow.com/questions/19543547/why-jdks-string-indexof-does-not-use-kmp
今天在leetcode上刷题,正好刷到查找字符串的题目,想到了以前了解的KMP和Boyer-Moore等算法。这两个及其类似的算法的时间复杂度都接近于O(n)。
后面自己又去看了下JDK的String类中的indexof方法的实现,发现很奇怪,仅仅只是用了暴力破解法,也就是最原始的实现,时间复杂度也到了O(n*m)。
String类的indexof(String s)方法中调用一下方法:
/** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * @param sourceOffset offset of the source string. * @param sourceCount count of the source string. * @param target the characters being searched for. * @param targetOffset offset of the target string. * @param targetCount count of the target string. * @param fromIndex the index to begin searching from. */ static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; int max = sourceOffset + (sourceCount - targetCount); for (int i = sourceOffset + fromIndex; i <= max; i++) { /* Look for first character. */ if (source[i] != first) { while (++i <= max && source[i] != first); } /* Found first character, now look at the rest of v2 */ if (i <= max) { int j = i + 1; int end = j + targetCount - 1; for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++); if (j == end) { /* Found whole string. */ return i - sourceOffset; } } } return -1; }
谷歌并翻了下StackOverflow:
原来JDK的编写者们认为大多数情况下,字符串都不长,使用原始实现可能代价更低。因为KMP和Boyer-Moore算法都需要预先计算处理来获得辅助数组,需要一定的时间和空间,这可能在短字符串查找中相比较原始实现耗费更大的代价。而且一般大字符串查找时,程序员们也会使用其它特定的数据结构,查找起来更简单。这有点类似于排除特定情况下的快速排序了。不同环境选择不同算法。
Reference:
http://stackoverflow.com/questions/19543547/why-jdks-string-indexof-does-not-use-kmp
相关文章推荐
- 为什么JDK中String类的indexof不使用KMP或者Boyer-Moore等时间复杂度低的算法编辑器
- Boyer-Moore文本匹配算法(联合使用KMP和Horspool算法)
- 归并排序的改进算法(使用静态链表)的时间复杂度分析
- 字符串匹配算法之KMP&Boyer-Moore
- [算法] 使用“复杂”的数据结构降低时间复杂度
- 浅谈字符串匹配的几种算法(KMP,Boyer-Moore)
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- 使用Origin画出复杂网络博弈中合作率时间演化图(学术论文)
- Boyer and Moore Fast majority vote algorithm(快速选举算法)
- PHP uniqid()函数可用于生成不重复的唯一标识符,该函数基于微秒级当前时间戳。在高并发或者间隔时长极短(如循环代码)的情况下,会出现大量重复数据。即使使用了第二个参数,也会重复,最好的方案是结
- 使用Python将时间或者时间间隔转为ISO 8601格式
- 算法运行时间复杂度
- 算法(一)时间复杂度
- Facebook开源时间序列内存数据库Beringei,追求极致压缩率——如果是int根据大多数时间序列中的值与相邻数据点相比并没有显著的变化,只要使用XOR将当前值与先前值进行比较,然后存储发生变化的比特。最终,该算法将整个数据集至少压缩了90%
- C++解决最大子列和问题,算法时间复杂度优化
- 页面置换算法--LFU算法实现-O(1)时间复杂度
- 每对顶点间的最短路径算法时间复杂度改进C++实现