java 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.

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.

解决方案

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, but from what I understand, none of the major relevant facts have changed since it was written.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值