Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When not to use RegexOptions.Compiled

Tags:

c#

regex

I understand the advantage of using RegexOptions.Compiled - it improves upon the execution time of app by having the regular expression in compiled form instead of interpreting it at run-time. Although using this is not recommended for application which are already slow at start-up time.

But if my application can bear any slight increase in start-up time -
what are the other scenarios in which I should NOT use RegexOptions.Compiled?

Just as a note I am calling this method several times -

private static string GetName(string objString) {     return Regex.Replace(objString, "[^a-zA-Z&-]+", ""); } 

So, this method is called with different values for 'objString' (although values for objString may repeat as well).

Do you think it's good/not good to use RegexOptions.Compiled here? Any web link would be really helpful.
Thank you!


EDIT

I tested my web app with both

  • RegexOptions.Compiled, and
  • Instantiate Regex as class variable

But couldn't find any big difference in time taken by my web application - Only thing I noticed in both scenarios is that first time when the application loads it's taking double of the time taken compared to that in successive page loads and that is irrespective of whether I use RegexOptions.Compiled or not.

Any comments for --
why my web application takes longer for the Regex to process for first time and time taken is reduced to almost half or less in subsequent loads - Is there any inbuilt caching or some other .net feature is helping here. P.S. This thing is same if I use RegexOptions.Compiled or not.

like image 684
inutan Avatar asked Apr 01 '12 23:04

inutan


People also ask

What is RegexOptions compiled?

RegexOptions. Compiled instructs the regular expression engine to compile the regular expression expression into IL using lightweight code generation (LCG). This compilation happens during the construction of the object and heavily slows it down. In turn, matches using the regular expression are faster.

What is a compiled regular expression?

The compile() function takes as input a simple regular expression and produces a compiled expression that can be used with the step() and advance() functions. The first parameter instring is never used explicitly by compile(). It is a pointer to a character string defining a source regular expression.

What is the most accurate description of a regular expression C#?

In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex. The . Net Framework provides a regular expression engine that allows the pattern matching.

What is regex option?

You can specify options for regular expressions in one of three ways: In the options parameter of a System. Text. RegularExpressions. Regex class constructor or static ( Shared in Visual Basic) pattern-matching method, such as Regex(String, RegexOptions) or Regex.


1 Answers

For any specific performance question like this, the best way to find out which way is faster is to test both and see.

In general, compiling a regex is unlikely to have much benefit unless you're using the regex a lot, or on very large strings. (Or both.) I think it's more of an optimization to try after you've determined that you have a performance problem and you think this might help, than one to try randomly.

For some general discussion on the drawbacks of RegexOptions.Compiled, see this blog post by Jeff Atwood; it's very old (from the days of .NET Framework 1.1), but from what I understand, none of the major relevant facts have changed since it was written.


  • Original blog link: https://blog.codinghorror.com/to-compile-or-not-to-compile/
  • Wayback Machine link (in case original link expires): https://web.archive.org/web/20140411223755/https://blog.codinghorror.com/to-compile-or-not-to-compile/
  • Original blog article heavily quotes this MSDN blog article: https://web.archive.org/web/20140413160809/http://blogs.msdn.com/b/bclteam/archive/2004/11/12/256783.aspx
like image 88
ruakh Avatar answered Sep 21 '22 11:09

ruakh