When should I use Regex over string operations and vice versa only regarding performance?
split is faster, but complex separators which might involve look ahead, Regex is only option.
The reason the regex is so slow is that the "*" quantifier is greedy by default, and so the first ". *" tries to match the whole string, and after that begins to backtrack character by character. The runtime is exponential in the count of numbers on a line.
Regex is instrinsically a process of pattern matching and should be used when the types of strings you want to match are variable or only conform to a particular pattern. For cases when a simple string search would suffice, I would always recommend using the in-built methods of the String class.
Regex is faster for large string than an if (perhaps in a for loops) to check if anything matches your requirement.
Although string manipulation will usually be somewhat faster, the actual performance heavily depends on a number of factors, including:
As the regex gets more complicated, it will take much more effort and complexity to write equivlent string manipulation code that performs well.
String operations will always be faster than regular expression operations. Unless, of course, you write the string operations in an inefficient way.
Regular expressions have to be parsed, and code generated to perform the operation using string operations. At best, the regular expression operation can do what's optimal to do the string manipulations.
Regular expressions are not used because they can do anything faster than plain string operations, it's used because it can do very complicated operations with little code, with reasonably small overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With