Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:57:50+00:00 2026-05-22T00:57:50+00:00

I run into this article: Performance: Compiled vs. Interpreted Regular Expressions , I modified

  • 0

I run into this article:

Performance: Compiled vs. Interpreted Regular Expressions, 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!

This means RegexOptions.Compiled option is completely useless, actually even worse, it’s slower! 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 @Jim in the answers provided a much cleaner version which works as expected.

Can anyone explain why this is the case?

Code, taken & modified from the blog post:

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 < 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 < 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<Regex> _expressions;
        private static object _SyncRoot = new object();

        private static List<Regex> GetExpressions()
        {
            if (_expressions != null)
                return _expressions;

            lock (_SyncRoot)
            {
                if (_expressions == null)
                {
                    DateTime startTime = DateTime.Now;

                    List<Regex> tempExpressions = new List<Regex>();
                    string regExPattern =
                        @"^[a-zA-Z0-9]+[a-zA-Z0-9._%-]*@{0}$";

                    for (int i = 0; i < 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<Regex>(tempExpressions);
                    DateTime endTime = DateTime.Now;
                    double msTaken = endTime.Subtract(startTime).TotalMilliseconds;
                    Console.WriteLine("Init:" + msTaken);
                }
            }

            return _expressions;
        }

        static  List<Regex> 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);

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:57:51+00:00Added an answer on May 22, 2026 at 12:57 am

    Compiled regular expressions match faster when used as intended. As others have pointed out, the idea is to compile them once and use them many times. The construction and initialization time are amortized out over those many runs.

    I created a much simpler test that will show you that compiled regular expressions are unquestionably faster than not compiled.

        const int NumIterations = 1000;
        const string TestString = "some random text with email address, address@domain200.com";
        const string Pattern = "^[a-zA-Z0-9]+[a-zA-Z0-9._%-]*@domain0\\.\\.com$";
        private static Regex NormalRegex = new Regex(Pattern, RegexOptions.IgnoreCase);
        private static Regex CompiledRegex = new Regex(Pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private static Regex DummyRegex = new Regex("^.$");
    
        static void Main(string[] args)
        {
            var DoTest = new Action<string, Regex, int>((s, r, count) =>
                {
                    Console.Write("Testing {0} ... ", s);
                    Stopwatch sw = Stopwatch.StartNew();
                    for (int i = 0; i < count; ++i)
                    {
                        bool isMatch = r.IsMatch(TestString + i.ToString());
                    }
                    sw.Stop();
                    Console.WriteLine("{0:N0} ms", sw.ElapsedMilliseconds);
                });
    
            // Make sure that DoTest is JITed
            DoTest("Dummy", DummyRegex, 1);
            DoTest("Normal first time", NormalRegex, 1);
            DoTest("Normal Regex", NormalRegex, NumIterations);
            DoTest("Compiled first time", CompiledRegex, 1);
            DoTest("Compiled", CompiledRegex, NumIterations);
    
            Console.WriteLine();
            Console.Write("Done. Press Enter:");
            Console.ReadLine();
        }
    

    Setting NumIterations to 500 gives me this:

    Testing Dummy ... 0 ms
    Testing Normal first time ... 0 ms
    Testing Normal Regex ... 1 ms
    Testing Compiled first time ... 13 ms
    Testing Compiled ... 1 ms
    

    With 5 million iterations, I get:

    Testing Dummy ... 0 ms
    Testing Normal first time ... 0 ms
    Testing Normal Regex ... 17,232 ms
    Testing Compiled first time ... 17 ms
    Testing Compiled ... 15,299 ms
    

    Here you see that the compiled regular expression is at least 10% faster than the not compiled version.

    It’s interesting to note that if you remove the RegexOptions.IgnoreCase from your regular expression, the results from 5 million iterations are even more striking:

    Testing Dummy ... 0 ms
    Testing Normal first time ... 0 ms
    Testing Normal Regex ... 12,869 ms
    Testing Compiled first time ... 14 ms
    Testing Compiled ... 8,332 ms
    

    Here, the compiled regular expression is 35% faster than the not compiled regular expression.

    In my opinion, the blog post you reference is simply a flawed test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into an InvalidProgramException. This article: http://support.microsoft.com/kb/312544/en-us Suggests I run PEVerify.exe, but I
I run into this quite often where a new page is supposedly tested and
Has anyone run into this error message before when using a timer on an
I've run into this issue quite a few times and never liked the solution
Has anyone else run into this problem before? I've got a method that calls
I seem to run into this very often. I need to build a Hash
Has anyone run into this problem... In my layout.phtml I have: <head> <?= $this->headTitle('Control
Has anyone run into this problem? I have a collection of comments that I
I have run into this problem before but never quite solved it. I have
I've run into this problems several times before when trying to do some html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.