If I have to find let's say a word in a sentence, i can think of two approaches
- Using string.IndexOf
- Using Regex
Which one is better in terms of performance or best practice
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.
"\bpart\b" will match the whole word "part" exactly once, and will likely be less expensive than a convoluted IndexOf algorithm.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.