Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regex class and RegexOptions.Compiled option

Regex class and RegexOptions.Compiled option

Scheduled Pinned Locked Moved C#
regexperformancequestioncode-review
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Callixte
    wrote on last edited by
    #1

    I have a question about Regex class and wether it is clerver to put that RegexOptions.Compiled option. Msdn says: To improve performance, the regular expression engine caches all regular expressions in memory. This avoids the need to reparse an expression into high-level byte code each time it is used. Does it mean that the Regex object is cached as itself or that another Regex with same pattern and options will use the cached one and will not need to parse the pattern again? If it is the first case, it means that compiling is useful only when the same object is used several times. If it is the latter, the compiled class will be used every time the same pattern is used. Thx. Callixte.

    C 1 Reply Last reply
    0
    • C Callixte

      I have a question about Regex class and wether it is clerver to put that RegexOptions.Compiled option. Msdn says: To improve performance, the regular expression engine caches all regular expressions in memory. This avoids the need to reparse an expression into high-level byte code each time it is used. Does it mean that the Regex object is cached as itself or that another Regex with same pattern and options will use the cached one and will not need to parse the pattern again? If it is the first case, it means that compiling is useful only when the same object is used several times. If it is the latter, the compiled class will be used every time the same pattern is used. Thx. Callixte.

      C Offline
      C Offline
      Curtis Schlak
      wrote on last edited by
      #2

      Callixte, I don't think that .NET pools Regex objects. It seems that using the RegexOptions.Compiled will only give you significant benefits if you reuse the same object. I wrote the following code to test for reference and object equality, to see if .NET pools the Regex object like it pools the String object in some internal cache. However, the code seems to imply that each object stands alone.

      using System;
      using System.Text.RegularExpressions;

      namespace ThrowAway
      {
      public class TMain
      {
      [STAThread]
      public static void Main()
      {
      Regex compiled1 = new Regex( "(a(b)+)+", RegexOptions.Compiled );
      Regex compiled2 = new Regex( "(a(b)+)+", RegexOptions.Compiled );
      Regex noncompiled1 = new Regex( "(a(b)+)+" );
      Regex noncompiled2 = new Regex( "(a(b)+)+" );
      Console.WriteLine( compiled1 == compiled2 ); // Writes False
      Console.WriteLine( compiled1.Equals( compiled2 ) ); // Writes False
      Console.WriteLine( compiled1 == noncompiled1 ); // Writes False
      Console.WriteLine( compiled1.Equals( noncompiled1 ) ); // Writes False
      Console.WriteLine( noncompiled1 == noncompiled2 ); // Writes False
      Console.WriteLine( noncompiled1.Equals( noncompiled2 ) ); // Writes False
      }
      }
      }

      Now, this doesn't really prove anything; however, I feel that, if the .NET runtime cached compiled1 and compiled2 to the same internal pool, then at lease compiled1 == compiled2 should evaluate to true. I hope that helps.

      C 1 Reply Last reply
      0
      • C Curtis Schlak

        Callixte, I don't think that .NET pools Regex objects. It seems that using the RegexOptions.Compiled will only give you significant benefits if you reuse the same object. I wrote the following code to test for reference and object equality, to see if .NET pools the Regex object like it pools the String object in some internal cache. However, the code seems to imply that each object stands alone.

        using System;
        using System.Text.RegularExpressions;

        namespace ThrowAway
        {
        public class TMain
        {
        [STAThread]
        public static void Main()
        {
        Regex compiled1 = new Regex( "(a(b)+)+", RegexOptions.Compiled );
        Regex compiled2 = new Regex( "(a(b)+)+", RegexOptions.Compiled );
        Regex noncompiled1 = new Regex( "(a(b)+)+" );
        Regex noncompiled2 = new Regex( "(a(b)+)+" );
        Console.WriteLine( compiled1 == compiled2 ); // Writes False
        Console.WriteLine( compiled1.Equals( compiled2 ) ); // Writes False
        Console.WriteLine( compiled1 == noncompiled1 ); // Writes False
        Console.WriteLine( compiled1.Equals( noncompiled1 ) ); // Writes False
        Console.WriteLine( noncompiled1 == noncompiled2 ); // Writes False
        Console.WriteLine( noncompiled1.Equals( noncompiled2 ) ); // Writes False
        }
        }
        }

        Now, this doesn't really prove anything; however, I feel that, if the .NET runtime cached compiled1 and compiled2 to the same internal pool, then at lease compiled1 == compiled2 should evaluate to true. I hope that helps.

        C Offline
        C Offline
        Callixte
        wrote on last edited by
        #3

        Thanks Curtis, My guess was that compiled1 and compiled2 would be of the same "class" dynamicly inherited of Regex, so CLR doesn't have to parse twice the expression et re-use the same byte code. Callixte.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups