Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's faster: Regex or string operations? [closed]

When should I use Regex over string operations and vice versa only regarding performance?

like image 904
Fabian Bigler Avatar asked May 19 '13 19:05

Fabian Bigler


People also ask

Is regex faster than string split?

split is faster, but complex separators which might involve look ahead, Regex is only option.

Is regex fast or slow?

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.

Are string functions better than regular expressions?

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.

Is regex faster than a for loop?

Regex is faster for large string than an if (perhaps in a for loops) to check if anything matches your requirement.


2 Answers

It depends

Although string manipulation will usually be somewhat faster, the actual performance heavily depends on a number of factors, including:

  • How many times you parse the regex
  • How cleverly you write your string code
  • Whether the regex is precompiled

As the regex gets more complicated, it will take much more effort and complexity to write equivlent string manipulation code that performs well.

like image 167
SLaks Avatar answered Sep 23 '22 01:09

SLaks


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.

like image 21
Guffa Avatar answered Sep 22 '22 01:09

Guffa