现在的位置: 首页 > 综合 > 正文

indexof未必比正则快,测试程序如下

2013年02月14日 ⁄ 综合 ⁄ 共 1599字 ⁄ 字号 评论关闭

indexof未必比正则快,测试程序如下:

1. 测试indexof, 循环100000次
private void button1_Click(object sender, System.EventArgs e)
{
String str1 = "Beginning with Windows 95 and Windows NT® 4.0, Input Method Editors (IMEs) are provided as a dynamic-link library (DLL), in contrast to the IMEs for the Windows 3.1 Far East Edition. Each IME runs as one of the multilingual keyboard layouts. In comparison to the Windows 3.1 IME, the new Win32 Multilingual Input Method Manager (IMM) and Input Method Editor (IME) architecture provide the following advantages:Run as a component of the multilingual environment";
String str2 = "dynamic-link";
bool flag = false;
long t1 = System.Environment.TickCount;
for (int i = 0; i < 100000; i++)
{
if (str1.IndexOf(str2) > -1)
{
flag = true;
}
}
long t2 = System.Environment.TickCount;
long t3 = t2 - t1;
MessageBox.Show(t3.ToString());
MessageBox.Show(flag.ToString());
}
2. 测试正则, 循环100000次
private void button2_Click(object sender, System.EventArgs e)
{
String str1 = "Beginning with Windows 95 and Windows NT® 4.0, Input Method Editors (IMEs) are provided as a dynamic-link library (DLL), in contrast to the IMEs for the Windows 3.1 Far East Edition. Each IME runs as one of the multilingual keyboard layouts. In comparison to the Windows 3.1 IME, the new Win32 Multilingual Input Method Manager (IMM) and Input Method Editor (IME) architecture provide the following advantages:Run as a component of the multilingual environment";
String str2 = "dynamic-link";
bool FoundMatch = false;
long t1 = System.Environment.TickCount;
for (int i = 0; i < 100000; i++)
{
try
{
FoundMatch = Regex.IsMatch(str1, str2);
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
}
long t2 = System.Environment.TickCount;
long t3 = t2 - t1;
MessageBox.Show(t3.ToString());
MessageBox.Show(FoundMatch.ToString());
}
测试结果,indexof稍慢, 不知道我上面的测试方法有没有错,大家看看

 

抱歉!评论已关闭.