Is there any difference in speed/memory usage for these two equivalent expressions:
Regex.IsMatch(Message, "1000")
Vs
Message.Contains("1000")
Any situations where one is better than other ?
The context of this question is as follows: I was making some changes to legacy code which contained the Regex expression to find whether a string is contained within another string. Being legacy code I did not make any changes to that and in the code review somebody suggested that Regex.IsMatch should be replaced by string.Contains. So I was wondering whether the change was worth making.
With all that said, String. Contains and String. IndexOf is only useful for checking the existence of an exact substring, but Regex is much more powerful and allows you to do so much more.
NET 4.0 - IndexOf no longer uses Ordinal Comparison and so Contains can be faster.
IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options and time-out interval.
String operations will always be faster than regular expression operations.
For simple cases String.Contains
will give you better performance but String.Contains
will not allow you to do complex pattern matching. Use String.Contains
for non-pattern matching scenarios (like the one in your example) and use regular expressions for scenarios in which you need to do more complex pattern matching.
A regular expression has a certain amount of overhead associated with it (expression parsing, compilation, execution, etc.) that a simple method like String.Contains
simply does not have which is why String.Contains
will outperform a regular expression in examples like yours.
String.Contains
is slower when you compare it to a compiled regular expression. Considerably slower, even!
You can test it running this benchmark:
class Program { public static int FoundString; public static int FoundRegex; static void DoLoop(bool show) { const string path = "C:\\file.txt"; const int iterations = 1000000; var content = File.ReadAllText(path); const string searchString = "this exists in file"; var searchRegex = new Regex("this exists in file"); var containsTimer = Stopwatch.StartNew(); for (var i = 0; i < iterations; i++) { if (content.Contains(searchString)) { FoundString++; } } containsTimer.Stop(); var regexTimer = Stopwatch.StartNew(); for (var i = 0; i < iterations; i++) { if (searchRegex.IsMatch(content)) { FoundRegex++; } } regexTimer.Stop(); if (!show) return; Console.WriteLine("FoundString: {0}", FoundString); Console.WriteLine("FoundRegex: {0}", FoundRegex); Console.WriteLine("containsTimer: {0}", containsTimer.ElapsedMilliseconds); Console.WriteLine("regexTimer: {0}", regexTimer.ElapsedMilliseconds); Console.ReadLine(); } static void Main(string[] args) { DoLoop(false); DoLoop(true); return; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With