zoukankan      html  css  js  c++  java
  • 时间测试

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 时间测试
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] numbs = new int[100000];
    
                BuildArray(numbs);
    
                TimeSpan duration;
    
                DisplayNumbs(numbs);
                DisplayNumbs(numbs);
                DisplayNumbs(numbs);
                duration = Process.GetCurrentProcess().TotalProcessorTime;
    
                Console.WriteLine("Time:" + duration.TotalSeconds);
            }
    
            public static void BuildArray(int[] numbs)
            {
                for (int i = 0; i < numbs.Length; i++)
                {
                    numbs[i] = i;
                }
            }
    
            public static void DisplayNumbs(int[] numbs)
            {
                for (int i = 0; i < numbs.Length; i++)
                {
                    Console.Write(numbs[i] + " ");
                }
            }
        }
    }
    

    -------------------封装的Timing类----------------------------------------------------------------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Timing类
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] numbs = new int[100000];
    
                BuildArray(numbs);
    
                Timing test = new Timing();
    
                test.StartTime();
              
                DisplayNumbs(numbs);
                test.StopTime();
    
    
                Console.WriteLine("Time:" + test.Result());
            }
    
            public static void BuildArray(int[] numbs)
            {
                for (int i = 0; i < numbs.Length; i++)
                {
                    numbs[i] = i;
                }
            }
    
            public static void DisplayNumbs(int[] numbs)
            {
                for (int i = 0; i < numbs.Length; i++)
                {
                    Console.Write(numbs[i] + " ");
                }
            }
        }
    
        /// <summary>
        /// 时间测试类
        /// </summary>
        public class Timing
        {
            /// <summary>
            /// 记录开始时间
            /// </summary>
            private TimeSpan startingTime;
    
            /// <summary>
            /// 记录方法的用时
            /// </summary>
            private TimeSpan duration;
    
            /// <summary>
            /// 初始化
            /// </summary>
            public Timing()
            {
                startingTime = new TimeSpan(0);
                duration = new TimeSpan(0);
            }
    
            /// <summary>
            /// 结束计时
            /// </summary>
            public void StopTime()
            {
                duration = Process.GetCurrentProcess().Threads[0]
                    .UserProcessorTime.Subtract(startingTime);
            }
    
            /// <summary>
            /// 开始计时
            /// </summary>
            public void StartTime()
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
            }
    
            /// <summary>
            /// 获取结果
            /// </summary>
            /// <returns></returns>
            public TimeSpan Result()
            {
                return duration;
            }
        }
    }
    


  • 相关阅读:
    Swap Nodes in Pairs
    Length of Last Word
    leetcode开篇
    数据备份crontab+pyhon
    hdu 1848 博弈 难题 取石子 三堆
    jd1338.c
    ubuntu 13.10上配置打印机
    运煤智力题
    【开发记录】RTMP推流协议互联网直播点播平台EasyDSS新增生成水印模块记录
    互联网视频直播点播分析平台EasyDSS配置公网IP后登录提示“服务不存在”问题排查过程介绍
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671525.html
Copyright © 2011-2022 走看看