In this trivial example, you should not see most of the difference in performance, but the regular expression is involved purely from the algorithms
wordA|wordB
it really will be faster, because it just skips one pass through the string and uses a state machine to match one of the two substrings. However, this is first compensated by the construction of a finite state machine, which in this case should be fairly linear along the length of the regular expression. You can compile the regular expression first so that it costs only once while the compiled object lives.
Thus, essentially the cost is reduced to:
- linear search on a line twice (2 โข line length)
- or a linear search string once and building a DFA (string length + length of the regular expression)
if your text is very large, and the substring is very small, then it may be worth.
However, you are most likely optimizing the wrong place. Use the profiler to find actual bottlenecks in the code and optimize them; never worry about such trivial โoptimizationsโ if you cannot prove to them that they affect.
Finally, we must consider the following: a regular expression, you can be sure that you actually meet the words (or things like words), not according to what may be the real reason for the consideration of a regular expression instead of contains .
source share