Stopwatch的用法

今天在sohighthesky的博客上看到了几行代码,其中使用了Stopwatch函数,猜测可能是用来计算一个函数的运行所需时间的.代码如下:

            Stopwatch wat = new Stopwatch();
            wat.Start();

            for (int i = 1; i < 100000; i++)
            {
                res1 = str.isNumberic1();
            }
            wat.Stop();
            Console.WriteLine("isDigit      {0}:{1},{2}", str, wat.ElapsedMilliseconds, res1);

            wat.Reset();
            wat.Start();
            for (int i = 1; i < 100000; i++)
            {
                res2= str.isNumberic2();
            }
            wat.Stop();
            Console.WriteLine("isNumberic   {0}:{1},{2}", str, wat.ElapsedMilliseconds, res2);

wat.ElapsedMilliseconds返回值就是在wat.Start()和wat.Stop()之间函数运行的时间

posted on 2010-02-01 13:53  魔法皇帝  阅读(2570)  评论(0编辑  收藏  举报