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");

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;
      for (int i = 0; i < trials; i++) {
        bool isMatch = Regex.IsMatch(message, "nulla");
      Console.WriteLine("regex = " + s.Elapsed);
      for (int i = 0; i < trials; i++) {
        bool isMatch = message.Contains("nulla");
      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


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)
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.”


Author: natebrix

Follow me on twitter at @natebrix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s