当不使用RegexOptions.Compiled [英] When not to use RegexOptions.Compiled

查看:308
本文介绍了当不使用RegexOptions.Compiled的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解使用RegexOptions.Compiled的优势 -
它通过在编译的形式正则表达式,而不是在运行时解释它改进了应用程序的执行时间。虽然不推荐应用程序,它已经在启动时间慢用这个

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.

但是,如果我的应用程序能承受的启动时间的小幅增长 -

什么其他的方案中,我不应使用RegexOptions.Compiled

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&-]+", "");
}



所以,这种方法被称为与objString'不同的值(虽然值对于objString可重复以及)。

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

你觉得这是很好/不好用在这里RegexOptions.Compiled?
任何网站链接将是非常有益的。结果
谢谢!

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

修改

我测试了我的web应用程序既

I tested my web app with both


  • RegexOptions.Compiled

  • 实例化正则表达式作为类变量

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

但无法找到任何大的时候我的web应用程序所采取的差异 -
我唯一在这两种情况下注意到的是,第一次时值应用程序加载它采取双重的所花费的时间相比,在连续的页面加载,这是无论我是否使用RegexOptions.Compiled与否。

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.

任何意见对 - 结果
为什么我的web应用程序需要较长时间的正则表达式,以便在后续的负载减少到几乎一半或更少采取第一次处理 - 是否有任何内建缓存或一些其他.NET功能帮助在这里。
附:如果我使用RegexOptions.Compiled与否这件事是一样的。

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.

推荐答案

对于这样的具体表现问题,以最好的方式找出哪些方法是快是来测试看看。

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.

有关 RegexOptions.Compiled 的弊端一些一般性的讨论,请参阅的这个博客帖子由杰夫·阿特伍德;这是很老了,但是从我个人理解,没有任何主要的相关事实已经改变,因为它是写的。

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

这篇关于当不使用RegexOptions.Compiled的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆