zoukankan      html  css  js  c++  java
  • (42)C#Stopwatch类(计算程序运行时间)

    引入命名空间 using System.Diagnostics;

            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 10000000; i++);
                sw.Stop();
                Console.WriteLine(sw.Elapsed);//显示程序执行的分钟数,执行的时间如果是分钟以上级别的用这个比较合适
                Console.WriteLine(sw.ElapsedMilliseconds);//显示程序执行的秒数
                Console.ReadKey();
            }

    记录代码耗时

     class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("start...");
                TestTime tt = new TestTime();
                Thread.Sleep(3000);
                tt.writeTime("");
                Thread.Sleep(5000);
                tt.writeTime("");
                Thread.Sleep(2000);
                tt.writeTime("");
                Console.WriteLine("end...");
            }
        }
    
    
        public class TestTime
        {
            Stopwatch sw;
            string path = @"D:	ime.txt";
            //统计次数
            int count = 0;
            //上次时间
            long lastTime = 0;
            public  TestTime()
            {
                sw = new Stopwatch();
    
                if (File.Exists(path))
                {
                    File.WriteAllText(path, "");
                }
                else
                {
                    File.Create(path).Close();
                }
    
                sw.Start();
            }
    
            public void writeTime(string str)
            {
                count++;
                File.AppendAllText(path, "" + count.ToString() + "调用," + "距离上次时间:" + (sw.ElapsedMilliseconds - lastTime) / 1000 + "秒," + str + "
    ");
                lastTime = sw.ElapsedMilliseconds;
            }
        }
  • 相关阅读:
    第七周——上课笔记(一)
    第七周学习视频(二)
    第七周学习视频(一)
    第六周——上课笔记(二)
    第六周——上课笔记(一)
    第五周——上课笔记(二)
    第五周——上课笔记(一)
    第六周学习视频(一)
    iOS
    socket连接方式
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/8491744.html
Copyright © 2011-2022 走看看