开发者

RegEx vs string manipulation functions: What is better

If I have to find let's say a word in a sentence, i can think of two approaches

  1. Using string.Index开发者_开发知识库Of
  2. Using Regex

Which one is better in terms of performance or best practice


If it's fairly straightforward to do something without regex, it's almost always cheaper that way. String.IndexOf (or String.Contains) is definitely an example of this.


It depends on your exact requirements. If you truly need to find a word in a sentence (not a substring), then I believe that could be expressed more concisely and more explicitly using a well-named regex pattern than using IndexOf plus all the extra logic to make sure you're actually getting a complete single word.

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


This is by no means the most scientific way of measuring things but here is a bit of source code that indicates (under very specific constraints) regex is about 4 times slower then 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();
}
}


In terms of best practices, string.IndexOf is probably a little more obvious to someone reading the code. People's brains tend to close up as soon as they see a regular expression, so something straight-forward like IndexOf would keep their brains open.

As for performance, that's dependent on a lot of things and can only be properly answered through benchmarking of specific code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜