Stopwatch秒表的使用

在每次计算一段时间的时候,我们都要创建一个秒表,秒表不能重复使用。

如果 想重复使用该秒表那么应该重置秒表。

举例说明:

 static  string strmapth = @"Data Source=.SQLEXPRESS;AttachDbFilename=F:C#练习代码ConsoleApplication1ConsoleApplication1Testmdf.mdf;Integrated Security=True;User Instance=True";
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection(strmapth);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 300000; i++)
            {
                conn.Open();
                conn.Close();
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed .ToString ());
            Console.WriteLine("打开连接成功!");
            

            //重置sw秒表
            sw.Reset();
            sw.Start();
            conn.Close();
            conn.Dispose();
            sw.Stop();
            Console.WriteLine(sw.Elapsed.ToString());

            //创建一个新的秒表
            Stopwatch sw1 = new Stopwatch();
            sw1.Start();
            conn.Close();
            conn.Dispose();
            sw1.Stop();
            Console.WriteLine(sw1.Elapsed.ToString());


            Console.ReadLine();
        }
原文地址:https://www.cnblogs.com/yinyakun/p/3270213.html