2 ms·
Two-Way is quite old, but you're right that it is lesser known that BM and KMP. You'll find Two-Way implemented most commonly in environments where either alloc
by burntsushi 6y ago
Two-Way is quite old, but you're right that it is lesser known that BM and KMP. You'll find Two-Way implemented most commonly in environments where either allocation is undesirable or where you want as quick initialization as possible. Namely, Two-Way uses constant space, regardless of the size of your needle, where as BM generally requires space proportional to the size of your needle (and thus, allocation or some fixed upper limit on the needle size).
These kind of conditions occur in libc implementations where the library interfaces don't provide separate initialization and search phases; all you get is a substring search routine. (The interface is limiting; you can never avoid the overhead of constructing the searcher for a particular needle, even if you are using the same needle to search many different short haystacks.)
Whether one is better than the other is hard to say. Substring search routines these days are dominated by vector instructions and how effective you use them in the common cases.
KMP is basically never used. I can't think of a single place where it is used off the top of my head.