title: Why Compiled RegEx performance is slower than Intrepreted RegEx?
body: <p>I run into this article:</p>
  
  <p><a href="http://webcache.googleusercontent.com/search?q=cache:allben.net/post/2009/08/06/Performance-Compiled-vs-Interpreted-Regular-Expressions.aspx" rel="noreferrer">Performance: Compiled vs. Interpreted Regular Expressions</a>, I modified the sample code to compile 1000 Regex and then run each 500 times to take advantage of precompilation, however even in that case interpreted RegExes run 4 times faster!</p>
  
  <p><strike>This means <code>RegexOptions.Compiled</code> option is completely useless, actually even worse, it's slower!</strike> Big difference was due to JIT, after solving JIT compiled regex in the the following code still performs a little bit slow and doesn't make sense to me but <a href="https://stackoverflow.com/questions/6004819/why-compiled-regex-performance-is-slower-than-intrepreted-regex/6005226#6005226">@Jim in the answers provided a much cleaner version which works as expected</a>.</p>
  
  <p>Can anyone explain why this is the case? </p>
  
  <p><strong>Code, taken &amp; modified from the blog post:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Text.RegularExpressions;
  
  namespace RegExTester
  {
      class Program
      {
          static void Main(string[] args)
          {
              DateTime startTime = DateTime.Now;
  
              for (int i = 0; i &lt; 1000; i++)
              {
                  CheckForMatches("some random text with email address, address@domain200.com" + i.ToString());    
              }
  
  
              double msTaken = DateTime.Now.Subtract(startTime).TotalMilliseconds;
              Console.WriteLine("Full Run: " + msTaken);
  
  
              startTime = DateTime.Now;
  
              for (int i = 0; i &lt; 1000; i++)
              {
                  CheckForMatches("some random text with email address, address@domain200.com" + i.ToString());
              }
  
  
              msTaken = DateTime.Now.Subtract(startTime).TotalMilliseconds;
              Console.WriteLine("Full Run: " + msTaken);
  
              Console.ReadLine();
  
          }
  
  
          private static List&lt;Regex&gt; _expressions;
          private static object _SyncRoot = new object();
  
          private static List&lt;Regex&gt; GetExpressions()
          {
              if (_expressions != null)
                  return _expressions;
  
              lock (_SyncRoot)
              {
                  if (_expressions == null)
                  {
                      DateTime startTime = DateTime.Now;
  
                      List&lt;Regex&gt; tempExpressions = new List&lt;Regex&gt;();
                      string regExPattern =
                          @"^[a-zA-Z0-9]+[a-zA-Z0-9._%-]*@{0}$";
  
                      for (int i = 0; i &lt; 2000; i++)
                      {
                          tempExpressions.Add(new Regex(
                              string.Format(regExPattern,
                              Regex.Escape("domain" + i.ToString() + "." +
                              (i % 3 == 0 ? ".com" : ".net"))),
                              RegexOptions.IgnoreCase));//  | RegexOptions.Compiled
                      }
  
                      _expressions = new List&lt;Regex&gt;(tempExpressions);
                      DateTime endTime = DateTime.Now;
                      double msTaken = endTime.Subtract(startTime).TotalMilliseconds;
                      Console.WriteLine("Init:" + msTaken);
                  }
              }
  
              return _expressions;
          }
  
          static  List&lt;Regex&gt; expressions = GetExpressions();
  
          private static void CheckForMatches(string text)
          {
  
              DateTime startTime = DateTime.Now;
  
  
                  foreach (Regex e in expressions)
                  {
                      bool isMatch = e.IsMatch(text);
                  }
  
  
              DateTime endTime = DateTime.Now;
              //double msTaken = endTime.Subtract(startTime).TotalMilliseconds;
              //Console.WriteLine("Run: " + msTaken);
  
          }
      }
  }
  </code></pre>