Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the performance of regular expressions in .NET programs

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to analyze the performance of regular expressions in .NET programs. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The RegexOptions.Compiled option was mistakenly used in the web program before, resulting in a slow opening of each page by nearly half a second..

Because the keywords are filtered in httpModule, it is sensitive to performance, so a simple test is done.

Stopwatch w = new Stopwatch (); w.Start (); Regex r = new Regex (@ ". *", RegexOptions.Compiled); Match m = r.Match ("aaa"); if (m.Success) {w.Stop (); Response.Write (w.Elapsed.TotalSeconds.ToString ("0.0000000")) }

Without RegexOptions.Compiled, the timer shows 0.0000x, and the counter shows 0.0000x after adding RegexOptions.Compiled

If the regular expression is more complex, the compilation time will only take 0.x seconds. It seems that most applications using RegexOptions.Compiled will degrade performance, unless it is a very BT application.

The regular expression of. Net uses the NFA engine, and the regular writing of the NFA engine is very good for performance.

Regex r = new Regex (@ "(a | [^ a] +) * ^"); string str= "11111111111111111111111111111111111111111111111111111111111111111"; Match m = r.Match (str); if (m.Success) {Response.Write (w.Elapsed.TotalSeconds.ToString ("0.0000000"));}

It is difficult to match as long as the length of str is more than 26.

The following are all similar

(B | [^ b] +) * ^ (c | [^ c] +) * ^ (^ | [^] +) * (^ | [^] +) * ^ (^ | [^] + | [^] *) * ^

It seems that regular writing has a great impact on performance.

This is the end of the problem about how to analyze the performance of regular expressions in .NET programs. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 260

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report