|
說到C#的Regex,談到最多的應(yīng)該就是RegexOptions.Compiled這個(gè)東西,傳說中在匹配速度方面,RegexOptions.Compiled是可以提升匹配速度的,但在啟動(dòng)速度上,使用了RegexOptions.Compiled情況下,通常會(huì)使啟動(dòng)速度慢許多,據(jù)說最多是60倍。
進(jìn)行一組測(cè)試,有測(cè)試數(shù)據(jù),才有討論依據(jù)。
第一步,帖上測(cè)試硬件信息(呵呵,硬件有點(diǎn)爛:()
第二步,
a.測(cè)試在沒有使用RegexOptions.Compiled項(xiàng)時(shí)候的情況,隨意使用一些內(nèi)容,然后循環(huán)一萬次實(shí)例化正則表達(dá)式對(duì)象來匹配這些內(nèi)容。

protected void Page_Load(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
string content = webClient.DownloadString("http://www.cnblogs.com/tmyh/archive/2010/09/29/sqlindex_01.html");
Stopwatch watcher = new Stopwatch();
watcher.Start();
int i = 10000;
while (i > 0)
{
Regex rgx = new Regex("<div>.+?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
bool b1 = rgx.IsMatch(content);
Regex rgx2 = new Regex("<p>.+?</p>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
bool b2 = rgx2.IsMatch(content);
i--;
}
Response.Write(string.Concat("<div>", watcher.Elapsed.TotalSeconds.ToString("f7"), "</div>"));
}
NET技術(shù):淺談提升C#正則表達(dá)式效率,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。