zoukankan
html css js c++ java
转自Discuz!NT代震军的blog,修改名字为Regex的效率的奇怪的问题,哈哈
在.net1.
4322
和.net2.0下,如下代码运行速度差近50倍,至今还未找到原因,目前的解决方法是在静态构造函数中直接进行声明,以提升速度,如果大家有兴趣可以看一下
RegexOptions.Compiled 这个正则项,我猜在vs2005下的框架有一些变化。
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;
using
System.Text;
using
System.Text.RegularExpressions;
public
partial
class
admin_RetTest2 : System.Web.UI.Page
{
public
static
Regex[] r
=
new
Regex[
10
];
static
admin_RetTest2()
{
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);
}
public
string
strTemplate
=
"
adfadsf ,adfadf aasdfadfwfadf adfadfwacx adfrafdda swfdfad
"
;
protected
void
Page_Load(
object
sender, EventArgs e)
{
#region
Regex r1
=
new
Regex(
@"
<%template ([^\[\]\{\}\s]+)%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
Match m;
ShowTime(
"
无 RegexOptions.Compiled 开始
"
);
for
(
int
i
=
0
; i
<
100
; i
++
)
{
r1
=
new
Regex(
@"
<%template ([^\[\]\{\}\s]+)%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n
"
);
}
r1
=
new
Regex(
@"
<%loop ([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+)%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
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
"
);
}
r1
=
new
Regex(
@"
<%\/loop%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\t}\t//end loop\r\n
"
);
}
r1
=
new
Regex(
@"
<%if ([^\s]+)%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\tif(
"
+
m.Groups[
1
].ToString()
+
"
)\r\n\t{\r\n
"
);
}
r1
=
new
Regex(
@"
<%else%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\t}\r\n\telse\r\n\t{\r\n
"
);
}
r1
=
new
Regex(
@"
<%\/if%>
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\t}\t//end if\r\n
"
);
}
r1
=
new
Regex(
@"
(\{strtoint\(([^\s]+?)\)\})
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
, 0)
"
);
}
r1
=
new
Regex(
@"
(\{urlencode\(([^\s]+?)\)\})
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
Utils.UrlEncode(
"
+
m.Groups[
2
].ToString()
+
"
)
"
);
}
//
解析{var.a}
r1
=
new
Regex(
@"
(\{([^\.\[\]\{\}\s]+)\.([^\[\]\{\}\s]+)\})
"
, RegexOptions.IgnoreCase
|
RegexOptions.Singleline);
for
(m
=
r1.Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\
"
+
"
+ m.Groups[2].ToString() +
"
.ToString().Trim()
+
\
""
);
}
}
ShowTime(
"
无 RegexOptions.Compiled 结束
"
);
#endregion
ShowTime(
"
有 RegexOptions.Compiled 开始
"
);
for
(
int
i
=
0
; i
<
100
; i
++
)
{
for
(m
=
r[
0
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n
"
);
}
for
(m
=
r[
2
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
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
"
);
}
for
(m
=
r[
3
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\t}\t//end loop\r\n
"
);
}
for
(m
=
r[
4
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\tif(
"
+
m.Groups[
1
].ToString()
+
"
)\r\n\t{\r\n
"
);
}
for
(m
=
r[
5
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\t}\r\n\telse\r\n\t{\r\n
"
);
}
for
(m
=
r[
6
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\r\n\t}\t//end if\r\n
"
);
}
for
(m
=
r[
7
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
, 0)
"
);
}
for
(m
=
r[
8
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
Utils.UrlEncode(
"
+
m.Groups[
2
].ToString()
+
"
)
"
);
}
//
解析{var.a}
for
(m
=
r[
9
].Match(strTemplate); m.Success; m
=
m.NextMatch())
{
strTemplate
=
strTemplate.Replace(m.Groups[
0
].ToString(),
"
\
"
+
"
+ m.Groups[2].ToString() +
"
.ToString().Trim()
+
\
""
);
}
}
ShowTime(
"
有 RegexOptions.Compiled 结束
"
);
}
public
void
ShowTime(
string
name)
{
DateTime dt
=
DateTime.Now;
Response.Write(
"
<br>
"
+
name
+
dt.Minute.ToString()
+
"
分
"
+
dt.Second.ToString()
+
"
秒
"
+
dt.Millisecond.ToString()
+
"
毫秒
"
);
}
}
查看全文
相关阅读:
kobject.c 添加注释
DNS之四---实现DNS的转发功能
DNS之三-----实现DNS的TCP/UDP功能及子域委派
DNS之二---实现DNS主从复制
第十二周----chrony时间同步与Cobbler+PXE自动化安装
网络时间同步服务和chrony
第十一周----黑客加入黑名单及将普通用户授权root权限
第十周--IP监控任务脚本实现
ELK之十三----kibana dashboard(仪表盘)使用
vmware虚拟机下linux centos6.6只有lo,没有eth0网卡、随机分配ip地址,固定ip地址等问题
原文地址:https://www.cnblogs.com/kokoliu/p/610925.html
最新文章
CI框架结合jQuery实现上传多张图片即时显示
Web点击链接调起手机系统自带短信发短信
workermanPHP聊天框架项目windows环境部署实践
jQuery设置样式css
Linux中打开文件显示行号相关命令
PHP CI框架调试开启报错信息方法
CentOS7.X基于LAMP环境搭建cacti
CentOS7.x安装nginx
mysql的2种备份mysqldump 和 Xtrabackup
CentOS 7的Linux系统优化加固
热门文章
CentOS7.X+LAMP+zabbix4.2环境下搭建Grafana6.1数据库可视化
CentOS7.X+LAMP环境下安装zabbix4.X
Centos7.x固定网络IP配置
CentOS7.X版本系统的下载和安装
CentOS7.X 搭建LAMP
单片机按键学习总结
单片机串口收发仿真实验
class.c 添加中文注释(1)
char_dev.c 添加中文注释
map.c 添加注释
Copyright © 2011-2022 走看看