locked
does static Regex.Match(string input, string pattern, RegexOptions ops) cache ?

    Question

  • in msdn, it is said that static Regex.Match(string input, string pattern) caches for future uses,

    but it doesn't say whether static Regex.Match(string input, string pattern, RegexOptions ops) caches.

    Then does it cache? Does it help to promote performance at creation?

     

    Friday, April 29, 2011 4:55 AM

Answers

  • Hi,

    yes, it caches, too (according to ILSpy *g*). But it doesn't help you at first creation time.. Frankly, I never saw a performance boost because I cached the regex-creation.
    In almost every case you have to choose a better pattern (or an appropriate parser) to get better performance.

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by Adam_Turner Friday, April 29, 2011 12:20 PM
    • Marked as answer by Dr. Vader Saturday, April 30, 2011 10:55 AM
    Friday, April 29, 2011 8:22 AM

All replies

  • Hi,

    yes, it caches, too (according to ILSpy *g*). But it doesn't help you at first creation time.. Frankly, I never saw a performance boost because I cached the regex-creation.
    In almost every case you have to choose a better pattern (or an appropriate parser) to get better performance.

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by Adam_Turner Friday, April 29, 2011 12:20 PM
    • Marked as answer by Dr. Vader Saturday, April 30, 2011 10:55 AM
    Friday, April 29, 2011 8:22 AM
  • I tried myself. It caches , and it helps saving over 80% overall time costs.
    Friday, April 29, 2011 11:30 AM
  • I tried myself. It caches , and it helps saving over 80% overall time costs.

    Your results are based on 1 regex. The results vary drastically among more complicated regex's.

    Adam


    Ctrl+Z
    Friday, April 29, 2011 12:20 PM
  • ... and how do you use it?

    E.g don't create an (uncached) regex-object inside a loop.

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    Friday, April 29, 2011 2:22 PM