C# Stopwatch计时统计

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        /// <summary>
        /// Stopwatch计时统计
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var stopwatch = new Stopwatch();//记录时间
            stopwatch.Start();//开始计时
            //实例化类,为线程提供参数
            Demo d1 = new Demo(1, 4);
            Thread th1 = new Thread(new ThreadStart(d1.Start));
            th1.Start();
            Thread.Sleep(1000);
            long sum = 0;
            for(int i=0;i<=1000000;i++)
            {
                sum = sum + i;
            }
            Thread.Sleep(6000);//休眠6秒
            stopwatch.Stop();//结束计时
            Console.WriteLine("耗时(毫秒):" + stopwatch.ElapsedMilliseconds.ToString());//用时多少毫秒
            Console.WriteLine("sun="+sum);
            Console.ReadLine();
        }
    }
    class Demo
    {
        private int a;
        private int b;
        public Demo(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
        public void Start()
        {
            for (int i = a; i < b; i++)
            {
                Console.WriteLine(i.ToString());
            }

        }
    }
}

  

posted @ 2017-11-09 15:19  杜子烟  阅读(172)  评论(0编辑  收藏  举报