我有像下面顯示的代碼whcih正在做正則表達式比較,我想優化代碼。當我檢查.net分析器它顯示80%的時間在這裏花費,我檢查了我的正則表達式過濾器是否發生回溯,但沒有錯在正則表達式side.So這是現有的代碼正則表達式的性能問題?
public class TestFilter : SmoothFilter
{
public override bool Apply(Team liveEvent)
{
// Apply each individual piece of the filter using the Match method
if ((Filter == null) || (Filter.Length <= 0) || Match(Filter, pl.Instance))
{
}
private bool Match(string pattern, string inputdata)
{
bool vals = Regex.IsMatch(inputdata, pattern, RegexOptions.IgnoreCase);
return vals;
}
}
當我用Google搜索我看到的鏈接http://blog.kurtschindler.net/post/regular-expression-performance-comparisons,提到最好且快速的方法,如果你不採取初始啓動時間。所以我修改了我的代碼,如下所示。但我移動了我的系統,我沒有任何工具來配置文件,任何機構可以告訴我它是否真的改善?這個修改後的代碼如下
static readonly Regex regexForMethod_E = new Regex("pattern", RegexOptions.Compiled);
private bool Match(string pattern, string inputdata)
{
bool vals = regexForMethod_E.IsMatch("inputdata");
return vals;
}
編輯
我還是我不知道如何將正則表達式實例內match()方法
你可以寫自己的簡單的速度測試 - 例如:http://guyellisrocks.com/coding/speed-improvements-with-compiled-regex/比較性能 – dash 2012-07-18 07:29:44
你的問題可以取決於你的正則表達式模式。請添加更多關於你的模式的細節。 – Ria 2012-07-18 08:23:26
在這種情況下,爲什麼要使用正則表達式,如果你只是做一個簡單的比較平等? – 2012-07-18 09:10:42