判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)

来源:互联网 发布:始作俑者其无后乎解释 编辑:程序博客网 时间:2024/05/17 17:15
 摘要:文章:判断一个字符串是否全是数字的多种方法及其性能比较(C#实现) 摘要:在编程的时候,经常遇到要判断一个字符串中的字符是否全部是数字(0-9),本来是一个很容易实现的功能,但程序员首先会想到的,发表于北京联高软件有限公司技术文章栏目,代码以高亮显示。
关键字:字符, 数字, 性能, 判断, time, seconds, isnumeric, int, end, start, 函数, exception, environment, system, tickcount, str

在编程的时候,经常遇到要判断一个字符串中的字符是否全部是数字(0-9),本来是一个很容易实现的功能,但程序员首先会想到的是,这样简单的功能有没有现成的函数可以用呢?VB.NET中有个IsNumeric(object),C#中只有判断单个字符的Char.IsNumber(),IsNumeric可以判断double类型的数字字符串,但无法排除正负号和小数点,如果判断字符串是否是一个数的话用它挺合适,但不能用来判断字符串是否全部由数字组成的。没现成的方法了,只好自己写函数:
> 纯文本方式> 拷贝到裁剪板> 打印
  1. public static bool IsNum(String str)   
  2. {   
  3.      for(int i=0;i<str.Length;i++)   
  4.      {   
  5.           if(!Char.IsNumber(str,i))   
  6.           return false;   
  7.      }   
  8.      return true;   
  9. }   
  10. //或用正则表达式:"^\d+$"   
  11. //还可以用Int32.Parse()抛出的Exception来判断:  
  12. try  
  13. {   
  14.      Int32.Parse(toBeTested);   
  15. }   
  16. catch  
  17. {   
  18.      //发生了异常,那么就不是数字了。   
  19. }  

 

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

> 纯文本方式> 拷贝到裁剪板> 打印
  1. class ExapleOfLegalsoft   
  2. {   
  3.      public void fun()   
  4.      {   
  5.           Regex isNumeric = new Regex(@"^\d+$");   
  6.           int times = 10000000;   
  7.           int start, end;   
  8.           int i;   
  9.           string toBeTested = "6741s"; 
  10.           #region Test user function   
  11.           start = System.Environment.TickCount;   
  12.           for (i = 0; i < times; i++)   
  13.           {   
  14.                 TimingTest.IsNum(toBeTested);   
  15.           }   
  16.           end = System.Environment.TickCount;   
  17.           Console.WriteLine("User function Time: " + (end - start) / 1000.0 + " Seconds"); 
  18.           #endregion  
  19.           #region Test Regular Expression  
  20.           start = System.Environment.TickCount;   
  21.           for (i = 0; i < times; i++)   
  22.           {   
  23.                 isNumeric.IsMatch(toBeTested);   
  24.           }   
  25.           end = System.Environment.TickCount;   
  26.           Console.WriteLine("Regular Expression Time: " + (end - start) / 1000.0 + " Seconds"); 
  27.           #endregion  
  28.           #region Test Exception   
  29.           start = System.Environment.TickCount;   
  30.           for (i = 0; i < times / 100; i++)   
  31.           {   
  32.                 try  
  33.                 {   
  34.                      Int32.Parse(toBeTested);   
  35.                 }   
  36.                 catch  
  37.                 {   
  38.                      //发生了异常,那么就不是数字了。   
  39.                 }   
  40.           }   
  41.           end = System.Environment.TickCount;   
  42.           Console.WriteLine("Exception Time: " + (end - start) / 10.0 + " Seconds"); 
  43.           #endregion  
  44.           #region Test VB.NET IsNumeric()   
  45.           start = System.Environment.TickCount;   
  46.           for (i = 0; i < times / 10; i++)   
  47.           {   
  48.                 Microsoft.VisualBasic.Information.IsNumeric(toBeTested);   
  49.           }   
  50.           end = System.Environment.TickCount;   
  51.           Console.WriteLine("VB.NET IsNumeric() Time: " + (end - start) / 100.0 + " Seconds"); 
  52.           #endregion   
  53.      }   
  54. }  
因为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"的话,我们可以这样改:
> 纯文本方式> 拷贝到裁剪板> 打印
  1. public static bool IsNum(String str)   
  2. {   
  3.  for(int i=0;i<str.Length;i++)   
  4.  {   
  5.   if(str[i]<"0" || str[i]>"9")   
  6.     return false;   
  7.  }   
  8.  return true;   
  9. }  

 

测试结果也令我吃惊,这样比原来的IsNum速度提高了近10倍,平均执行时间是0.205秒!
结果全部出来了,该怎样选择大家心里肯定已经有数了,我也不需要再说什么了。

原创粉丝点击