Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

KMP is conceptually very cool, as well as other clever string searching algorithms (BM, RK, AC), though one of the questions for me always was if even its most efficient implementation wouldn't be always slower than executing a brute-force combination of REP CMPSL/CMPSB instructions (x86) for vast majority of searched strings?


The time complexity of KMP is O(n), while the complexity of your idea is O(n^2). Considering the fact that KMP is not even doing a lot of things in its inner loop, (few memory accesses), a rep cmpsb approach is really no match for it, even in the trivial cases.

So no, it's much faster.


I know about the time complexity, but my argument points to the CPU architecture and cache/branch prediction efficiency.

KMP has a lot of branching which is more expensive than a simple cache (line) hit, cache REP CMPSx doesn't have any branching, aborts immediately after a mismatch with only a single loop over the starting position, search is also pretty much linear. This is not a Turing machine where it is being executed ;-)

So in theory KMP is faster, in practice for not very large strings I am really not sure... There are plenty of optimal algorithms where the fixed cost is too high for majority of useful cases comparing to less optimal algorithms with very low fixed costs. Does KMP have as much "mechanical sympathy" to overcome specific machine code instructions for most frequent cases?


This depends completely on your needle and your haystack. If it's the string "ABC" in "QABC" then certainly the naive algorithm will be the fastest.

"Mechanical sympathy" unfortunately is easily misinterpreted to mean to only listen to the machine. Remember though that Martin Thompson is alluding to Jackie Stewart and Formula One racing. All of the cars competed on the same track. But you wouldn't enter a Formula One car for the Baja 1000.

In fact, I don't think you should use term unless you have a specific goal in mind. KMP is only best for certain classes of searches. Boyer-Moore is used for others. And there are plenty of other algorithms with there own pros and cons. See http://www-igm.univ-mlv.fr/~lecroq/string/index.html for descriptions.

Python, for example, uses (or used?) the algorithm described at http://effbot.org/zone/stringlib.htm , for the reasons listed therein.


As with most optimisations the only way to be sure is to try it. Big-O complexity says very little about actual runtime on real-world data.


You're asking a question that the experts have also asked, so it's definitely a good question. The algorithm you're talking about is also called the naive algorithm. It's much faster in practice than you might expect given its simplicity and poor worst-case performance. That's because pathological behavior doesn't actually happen that often, and most of the time it is quite efficient.

I'm not sure whether it or KMP would be faster. But it doesn't matter much in the real world, because even better algorithms, like Boyer-Moore and its derivatives are faster still, and they are used instead.


O(m*n) is not the same as O(n^2).


Yes, but if something is O(m*n) and m <= n (which is the case in string search), then it's also O(n^2). On the other hand, since m can be of the order of, say, n/2, it's not too confusing to say that naive string search is O(n^2), since it's actually Theta(n^2).


Yes, you are right of course.

I really just meant that "in practice" the growth of the string length (m) varies independently and common cases behave more like O(n) than O(n^2). Also that naive search is probably heavily optimized with SSE assembly instructions and cache tuned. I have implemented several of these algorithms in C and it is difficult to impossible to beat C's strstr consistently with a simple implementation, even on cases that start to make the naive algorithm really inefficient.

You'd most likely need a hybrid approach to make a good general purpose string search function that is effective across many domains.


It's the same when m=n.


Of course in string search it never is. In m=n case KMP would simply compare the first character, and if it doesn't match declare nothing was found.

Yes there'd be much more different instructions involved but I think KMP would start beating naive pretty quickly in the m=n case.


If you don't have the entire string in memory then Boyer-Moore is usually fastest because you can avoid doing a lot of I/O (since you skip comparing many subsequences, you don't have to read those subsequences in the first place). This is why grep is very fast given a static string pattern to search for, even on huge files, but grepping for a regex is dog slow.


Until they implement a JIT and compile the regex.


No, you're missing the point -- using a regex does not allow you to read in less than all of the input (in the best case, even). If you grep a 1GB file with a regex you must read 1GB from disk, no exceptions, and that's many orders of magnitude slower than anything on the CPU (or in memory). With Boyer-Moore you can do less than 100% of the file size in I/O, especially if the given pattern is long relative to the full text.


We have no context to the environment that those regexs are running. For all we know they could have implicit limits and be getting compiled down to FPGAs. I can guarantee you that those regexs aren't running unbounded.

No point was missed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: