RegEx vs string management functions: which is better

If I need to find a word in a sentence, I can introduce two approaches

  • Using string.IndexOf
  • Using Regex

Which one is better in terms of performance or best practice

+4
source share
4 answers

It depends on your exact requirements. If you really need to find a word in a sentence (rather than a substring), then I believe that this could be expressed more concisely and more explicitly using a well-defined regular expression pattern than using IndexOf plus all the additional logic to make sure you are actually getting the full single word.

On the other hand, if you're just looking for a substring, then IndexOf is far superior in performance and readability.

+3
source

If it’s easy enough to do something without regular expression, it’s almost always cheaper. String.IndexOf (or String.Contains ) is certainly an example of this.

+6
source

This is by no means the most scientific way of measuring things, but here is a bit of source code that indicates (with very specific limitations) a regular expression about 4 times slower than indexof.

 class Program { private const string Sentence = "The quick brown fox jumps over the lazy dog"; private const string Word = "jumps"; static void Main(string[] args) { var indexTimes = new List<long>(); var regexTimes = new List<long>(); var timer = new Stopwatch(); for (int i = 0; i < 1000; i++) { timer.Reset(); timer.Start(); Sentence.IndexOf(Word); timer.Stop(); indexTimes.Add(timer.ElapsedTicks); } Console.WriteLine(indexTimes.Average()); for (int i = 0; i < 1000; i++) { timer.Reset(); timer.Start(); Regex.Match(Sentence, Word); timer.Stop(); regexTimes.Add(timer.ElapsedTicks); } Console.WriteLine(regexTimes.Average()); Console.ReadLine(); } } 
+3
source

As for best practices, string.IndexOf is probably a little more obvious to someone reading the code. People’s brains tend to close as soon as they see a regular expression, so something as straightforward as IndexOf will keep their brains open.

As far as performance is concerned, it depends on many things and can be correctly responsibly by benchmarking certain code.

+1
source

Source: https://habr.com/ru/post/1339099/


All Articles