Which is faster: Regex.IsMatch or String.Contains?
On an internal message board here at work somebody asked:
> Is there any difference in speed/memory usage for these two equivalent expressions: >
Regex.IsMatch(Message, "1000");
Message.Contains("1000");
My guess is that Message.Contains() is faster because it likely involves less machinery. Let’s try it and see.
using System;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
namespace TryItAndSee {
class Program {
static void Main(string[] args) {
string message = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in"
+ " reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt"
+ " in culpa qui officia deserunt mollit anim id est laborum.";
Stopwatch s = new Stopwatch();
int trials = 1000000;
s.Start();
for (int i = 0; i < trials; i++) {
bool isMatch = Regex.IsMatch(message, "nulla");
}
s.Stop();
Console.WriteLine("regex = " + s.Elapsed);
s.Reset();
s.Start();
for (int i = 0; i < trials; i++) {
bool isMatch = message.Contains("nulla");
}
s.Stop();
Console.WriteLine("contains = " + s.Elapsed);
}
}
}
The output appears to confirm my guess, at least on this input:
regex = 00:00:01.2446435
contains = 00:00:00.5458883
UPDATE:
Niels Kuhnel reports the following:
“
Sure. But if you're using RegexOptions.Compiled then IsMatch is actually faster.
Try putting:
Regex nulla = new Regex("nulla", RegexOptions.Compiled);
// Normally we have a static Regex so it isn't fair to time the initialization
// (although it doesn't make a difference in this case)
s.Start();
for (int i = 0; i < trials; i++) {
bool isMatch = nulla.IsMatch(message);
}
I got:
regex = 00:00:00.6902234
contains = 00:00:00.8815885
(during 10 trials it was consistently faster)
Lesson must be that if you're searching for the same thing a lot, the dynamically compiled state machine provided by RegexOptions.Compiled is actually faster. Even if just searching for a simple string.”