我来自唐朝
在.net1.4322 和.net2.0下,如下代码运行速度差近50倍,至今还未找到原因,目前的解决方法是在静态构造函数中直接进行声明,以提升速度,如果大家有兴趣可以看一下
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
RegexOptions.Compiled 这个正则项,我猜在vs2005下的框架有一些变化。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Text;
using System.Text.RegularExpressions;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class admin_RetTest2 : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Regex[] r=new Regex[10];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static admin_RetTest2()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
r[0]=new Regex(@"<%template ([^\[\]\{\}\s]+)%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[1]=new Regex(@"<%template ([^\[\]\{\}\s]+)%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[2]=new Regex(@"<%loop ([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+)%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[3]= new Regex(@"<%\/loop%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[4] = new Regex(@"<%if ([^\s]+)%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[5] = new Regex(@"<%else%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[6] = new Regex(@"<%\/if%>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[7] = new Regex(@"(\{strtoint\(([^\s]+?)\)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[8] = new Regex(@"(\{urlencode\(([^\s]+?)\)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
r[9] = new Regex(@"(\{([^\.\[\]\{\}\s]+)\.([^\[\]\{\}\s]+)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string strTemplate = "adfadsf ,adfadf aasdfadfwfadf adfadfwacx adfrafdda swfdfad";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region
Regex r1 = new Regex(@"<%template ([^\[\]\{\}\s]+)%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ShowTime("无 RegexOptions.Compiled 开始");
for (int i = 0; i < 100; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
r1 = new Regex(@"<%template ([^\[\]\{\}\s]+)%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"<%loop ([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+)%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\tint " + m.Groups[1].ToString() + "__loop__id=0;\r\n\tforeach(DataRow " + m.Groups[1].ToString() + " in " + m.Groups[2].ToString() + ".Rows)\r\n\t{\r\n\t\t" + m.Groups[1].ToString() + "__loop__id++;\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"<%\/loop%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\t}\t//end loop\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"<%if ([^\s]+)%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\tif(" + m.Groups[1].ToString() + ")\r\n\t{\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"<%else%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\t}\r\n\telse\r\n\t{\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"<%\/if%>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\t}\t//end if\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"(\{strtoint\(([^\s]+?)\)\})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),", 0)");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
r1 = new Regex(@"(\{urlencode\(([^\s]+?)\)\})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"Utils.UrlEncode(" + m.Groups[2].ToString() + ")");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//解析{var.a}
r1 = new Regex(@"(\{([^\.\[\]\{\}\s]+)\.([^\[\]\{\}\s]+)\})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
for (m = r1.Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\" + " + m.Groups[2].ToString() + ".ToString().Trim() + \"");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
ShowTime("无 RegexOptions.Compiled 结束");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ShowTime("有 RegexOptions.Compiled 开始");
for (int i = 0; i < 100; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (m = r[0].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[2].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\tint " + m.Groups[1].ToString() + "__loop__id=0;\r\n\tforeach(DataRow " + m.Groups[1].ToString() + " in " + m.Groups[2].ToString() + ".Rows)\r\n\t{\r\n\t\t" + m.Groups[1].ToString() + "__loop__id++;\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[3].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\t}\t//end loop\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[4].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\tif(" + m.Groups[1].ToString() + ")\r\n\t{\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[5].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\t}\r\n\telse\r\n\t{\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[6].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\r\n\t}\t//end if\r\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[7].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(), ", 0)");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (m = r[8].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"Utils.UrlEncode(" + m.Groups[2].ToString() + ")");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//解析{var.a}
for (m = r[9].Match(strTemplate); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
"\" + " + m.Groups[2].ToString() + ".ToString().Trim() + \"");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
ShowTime("有 RegexOptions.Compiled 结束");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void ShowTime(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime dt = DateTime.Now;
Response.Write("<br>" + name + dt.Minute.ToString() +"分"+ dt.Second.ToString() +"秒" + dt.Millisecond.ToString()+"毫秒");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)