在编程的时候,经常遇到要判断一个字符串中的字符是否全部是数字(0-9),本来是一个很容易实现的功能,但程序员首先会想到的是,这样简单的功能有没有现成的函数可以用呢?VB.NET中有个IsNumeric(object),C#中只有判断单个字符的Char.IsNumber(),IsNumeric可以判断double类型的数字字符串,但无法排除正负号和小数点,如果判断字符串是否是一个数的话用它挺合适,但不能用来判断字符串是否全部由数字组成的。没现成的方法了,只好自己写函数:

public static bool IsNum(String str)
{
   for(int i=0;i    {
       if(!Char.IsNumber(str,i))
       return false;
   }
   return true;
}

或用正则表达式:"^\d+$"

还可以用Int32.Parse()抛出的Exception来判断:

try
{
   Int32.Parse(toBeTested);
}
catch
{
   //发生了异常,那么就不是数字了。
}

那么哪一种方法最好呢?各有优劣。我顺手写了一个程序对每一种方法所需要的时间进行了测试。测试程序Main()内容如下:

Regex isNumeric = new Regex(@"^\d+$");
int times = 10000000;
int start, end;
int i;
string toBeTested = "6741s";

#region Test user function
start = System.Environment.TickCount;
for(i=0; i{
TimingTest.IsNum(toBeTested);
}
end = System.Environment.TickCount;
Console.WriteLine("User function Time: " + (end-start)/1000.0 + " Seconds");
#endregion

#region Test Regular Expression
start = System.Environment.TickCount;
for(i=0; i{
isNumeric.IsMatch(toBeTested);
}
end = System.Environment.TickCount;
Console.WriteLine("Regular Expression Time: " + (end-start)/1000.0 + " Seconds");
#endregion

#region Test Exception
start = System.Environment.TickCount;
for(i=0; i{
try
{
 Int32.Parse(toBeTested);
}
catch
{
 //发生了异常,那么就不是数字了。
}

}
end = System.Environment.TickCount;
Console.WriteLine("Exception Time: " + (end-start)/10.0 + " Seconds");
#endregion

#region Test VB.NET IsNumeric()
start = System.Environment.TickCount;
for(i=0; i{
Microsoft.VisualBasic.Information.IsNumeric(toBeTested);
}
end = System.Environment.TickCount;
Console.WriteLine("VB.NET IsNumeric() Time: " + (end-start)/100.0 + " Seconds");
#endregion


   因为Exception所用的时间太长,所以只测试了1/100,这样不太严谨,但是数量级不会错的。

三次运行的结果是:
User function Time: 1.938 Seconds
Regular Expression Time: 11.921 Seconds
Exception Time: 600 Seconds
VB.NET IsNumeric() Time: 40.797 Seconds
User function Time: 1.953 Seconds
Regular Expression Time: 12.016 Seconds
Exception Time: 590.6 Seconds
VB.NET IsNumeric() Time: 40 Seconds
User function Time: 2.000 Seconds
Regular Expression Time: 12 Seconds
Exception Time: 595.3 Seconds
VB.NET IsNumeric() Time: 39.69 Seconds
平均时间:

1.964
11.979
595.3
40.162

速度之比依次约为:303 : 49.7 : 1 : 14.82

结果很明显,自定义函数速度最快,异常的速度最慢。假如不需要抛异常的话string toBeTested = "67412";结果就成了:

User function Time: 1.922 Seconds
Regular Expression Time: 9.64 Seconds
Exception Time: 3.1 Seconds
VB.NET IsNumeric() Time: 39.07 Seconds
速度之比依次约为:20.33 : 4.05 : 12.60 : 1

结论:

   自定义函数可以获得最大的灵活性和最高的性能,而且复杂性也不高,是最佳的方法。
   正则表达式法和IsNumeric速度在同一个数量级上,但正则表达式可以确定一种字符串的格式,比如规定一定要有或没有小数点等,而IsNumeric无法做到。
   使用异常是应该避免的。建议仅把Exception作为处理异常的一种手段,而不是作为控制流程的一种手段。测试也表明,当有异常抛出时,要消耗大量的资源。
   IsNumeric是现成的函数,用起来最省事,只能判断所给的参数是否是数值(boolean/byte/int16/int32/int64/single/double/decimal),无法作进一步的要求(是否有小数点等)。但IsNumeric的参数是object,不局限于string。

   写到这里,我不禁想,还有没有比自定义函数更快的方法呢?答案是肯定的。在前面的自定义函数中,用的是Char.IsNumber()函数,这个函数不仅能判断标准ASCII码中的'1',甚至对全角中文的'1'的判断也是true,可见Char.IsNumber()判断的是所有的Unicode字符中的数字,其他语言中的数字也包括了。假如我们只允许ASCII中的'1'的话,我们可以这样改:

public static bool IsNum(String str)
{
for(int i=0;i {
 if(str[i]<'0' || str[i]>'9')
  return false;
}
return true;
}

   测试结果也令我吃惊,这样比原来的IsNum速度提高了近10倍,平均执行时间是0.205秒!

   结果全部出来了,该怎样选择大家心里肯定已经有数了,我也不需要再说什么了。


Program | 评论(0) | 引用(37) | 阅读(5593)