by (鼠·神·泪) ( )
str的长度----1的时间----2的时间----3的时间(默认情况使用相邻不重复数字)
1 ---- 406 ---- 31 ---- 31
30 ---- 468 ---- 468 ---- 515
100---- 484 ---- 1406---- 1703
测试代码:
private void button9_Click(object sender, EventArgs e)
{
int count = 1000000;
bool bl = true;
string str = textBox1.Text; //如果你要测试,相应改一下
DateTime time1 = DateTime.Now;
for (int i = 0; i < count; i++)
{
bl = IsNum1(str);
}
DateTime time2 = DateTime.Now;
for (int i = 0; i < count; i++)
{
bl = IsNum2(str);
}
DateTime time3 = DateTime.Now;
for (int i = 0; i < count; i++)
{
bl = IsNum3(str);
}
DateTime time4 = DateTime.Now;
TimeSpan span1 = time2 - time1;
TimeSpan span2 = time3 - time2;
TimeSpan span3 = time4 - time3;
MessageBox.Show(span1.TotalMilliseconds + "\r\n"
+ span2.TotalMilliseconds + "\r\n"
+ span3.TotalMilliseconds);
}
Regex reg = new Regex(@"^\d$", RegexOptions.Compiled);
bool IsNum1(string str)
{
return reg.IsMatch(str);
}
bool IsNum2(string str)
{
foreach (char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
bool IsNum3(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}