博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#测试代码段运行速度

Posted on 2010-10-29 11:22  迷梦江南  阅读(1005)  评论(0编辑  收藏  举报

CS代码:


           Stopwatch sw = new Stopwatch();

    sw.Start();

            int sum = 0;
            for (int i = 0; i < 100000; i++)
            {
                sum += i;
            }
            sw.Stop();
            Label1.Text = "for循环:<br/>" + sw.Elapsed.ToString() + "<br/>";
            sw.Reset();

            sw.Start();
            sum = 0;
            int m = 100000;
            while (m != 0)
            {
                sum += m;
                m--;
            }
            sw.Stop();
            Label1.Text += "while循环:<br/>" + sw.Elapsed.ToString() + "<br/>";