计算 某段代码的运行时长

    static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();//开启计时
            for (int i = 0; i < 30; i++)
            {
                Thread.Sleep(100);
                //Console.WriteLine(i+);
            }
            sw.Stop();//结束计时

            var s = sw.ElapsedMilliseconds;

            Console.WriteLine("耗时:" + s + "毫秒");

            Console.ReadKey();
        }

如上小demo,可以手动计算出耗时代码

posted on 2022-04-01 17:09  泰坦尼克号上的活龙虾  阅读(30)  评论(0)    收藏  举报

导航