zoukankan      html  css  js  c++  java
  • 数据结构和算法 – 番外篇.时间测试类Timing

    public class Timing
        {
            //startingTime--用来存储正在测试的代码的开始时间。
            TimeSpan startingTime;
            //duration——用来存储正在测试的代码的终止时间。
            TimeSpan durantion;
            public Timing()
            {
                startingTime = new TimeSpan(0);
                durantion = new TimeSpan(0);
            }
    
            public void startTime()
            {
                //先强制对所有代码进行回收
                GC.Collect();
                //挂起当前线程,直到处理终结器队列的线程清空该队列为止
                GC.WaitForPendingFinalizers();
                //获取关联程序运行代码所用的时间
                startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
            }
    
            public void StopTime()
            {
                durantion = Process.GetCurrentProcess().Threads[0].
                    UserProcessorTime.Subtract(startingTime);
            }
    
    
            public TimeSpan Result()
            {
                return durantion;
            }
        }

     

     

     

    测试

    static void Main(string[] args)
            {
                //DateTime starttime = DateTime.Now;
                //Print(10000);
                ////PrintN(100000);
                //DateTime endtime = DateTime.Now;
    
                //double end = TimeHelp.Service.Timehelp(starttime, endtime);
                //Console.WriteLine("耗时:" + end);
    
                Timing tObj = new Timing();
                tObj.startTime();
                Print(500000);
                tObj.StopTime();
                Console.WriteLine("耗时:" + tObj.Result().TotalSeconds);
                Console.Read();
            }
    
            public static void Print(int N)
            {
                for (int i = 0; i <= N; i++)
                {
                    Console.WriteLine(i);
                }
                return;
            }
  • 相关阅读:
    冲刺(五)
    第九周总结
    冲刺(四)
    2020年寒假假期总结0114
    2020年寒假假期总结0113
    大二暑假第一周总结--初次安装配置Hadoop
    2020年寒假假期总结0112
    大三课堂测试总结20191113
    大二暑假第七周总结--开始学习Hadoop基础(六)
    大二暑假第六周总结--开始学习Hadoop基础(五)
  • 原文地址:https://www.cnblogs.com/tangge/p/5442583.html
Copyright © 2011-2022 走看看