Regex class and RegexOptions.Compiled option
-
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. -
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.Callixte, I don't think that .NET pools
Regex
objects. It seems that using theRegexOptions.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 theRegex
object like it pools theString
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
andcompiled2
to the same internal pool, then at leasecompiled1 == compiled2
should evaluate totrue
. I hope that helps. -
Callixte, I don't think that .NET pools
Regex
objects. It seems that using theRegexOptions.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 theRegex
object like it pools theString
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
andcompiled2
to the same internal pool, then at leasecompiled1 == compiled2
should evaluate totrue
. I hope that helps.