生命无常
如果没有明天

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace IsNum
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Button button1;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(0, 16);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(232, 23);
   this.label1.TabIndex = 0;
   this.label1.Text = "label1";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(0, 40);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(232, 23);
   this.label2.TabIndex = 1;
   this.label2.Text = "label2";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(120, 128);
   this.button1.Name = "button1";
   this.button1.TabIndex = 2;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {


  }

  public void Test1(int count)
  {
   string testData="123456789925465465465";
   DateTime t1=System.DateTime.Now;
   for(int i=0;i<count;i++)
   {
    IsNum1(testData);
   }
   DateTime t2=System.DateTime.Now ;
   this.label1.Text ="Test1:"+(t2 -t1  ).ToString();
   
  }

  public void Test2(int count)
  {
   string testData="123456789925465465465";
   DateTime t1=System.DateTime.Now ;
   for(int i=0;i<count;i++)
   {
    IsNum2(testData);
   }
   DateTime t2=System.DateTime.Now ;
   this.label2.Text ="Test2:"+(t2 -t1  ).ToString();
   
  }

  public static bool IsNum1(string str)
  {
   for(int i=0;i<str.Length;i++)
   {
    if(str[i]<='0' || str[i]>='9')
     return false;
   }
   return true;
  }

  
  protected static bool IsDigit(char ch)
  {
   switch (ch)
   {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    {
     return true;
    }
   }
   return false;
  }
  public static bool IsNum2(string str)
  {
   for(int i=0;i<str.Length;i++)
   {
    if(  IsDigit(str[i])  ) return false;
   }
   return true;
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   int count=100000;
   Test1(count);

   Test2(count);
  
  }
 

 }
}

测试结果:
IsNum2的性能要好于IsNum1

posted on 2006-02-22 21:43  John  阅读(801)  评论(1编辑  收藏  举报