Home > Backend Development > C#.Net Tutorial > Describing data structures with C# 1: Detailed code explanation of statistical code execution time objects

Describing data structures with C# 1: Detailed code explanation of statistical code execution time objects

黄舟
Release: 2017-03-08 11:47:18
Original
1156 people have browsed it

Customize a class that counts code execution time CalcTiming, taking into account:

  • During the statistical execution process, no Garbage collection. That is, let the GC complete garbage collection before counting the code execution time.

  • Make sure that the statistics are the execution time of the code in the current process.

    Write the code as follows:

    public class CalcTiming
    {
        TimeSpan startTime;        
        private TimeSpan endTime;
        TimeSpan duration;        
        public CalcTiming()
        {
            startTime = new TimeSpan(0);
            duration = new TimeSpan(0);
        }        public void StartTime()
        {            //强制对所有代进行垃圾回收
            GC.Collect();            //挂起线程,终结器线程清空该队列为止
            GC.WaitForPendingFinalizers();            //用户运行代码的起始时间   
            startTime = Process.GetCurrentProcess().UserProcessorTime; 

        }        //统计的代码执行完毕的时间
        public void StopTime()
        {
            endTime = Process.GetCurrentProcess().UserProcessorTime;

            duration = endTime.Subtract(startTime);           
        }        //返回统计的代码执行消耗的时间
        public TimeSpan Result()
        {            return duration;
        }

    }
Copy after login

Use the above CalcTiming class to statistically display the time consumption of the array:

    class Program
    {
        static void Main(string[] args)
        {            int[] nums = new int[100000];
            BuildArray(nums);
            CalcTiming calcTime= new CalcTiming();
            calcTime.StartTime();
            DisplayNums(nums);
            calcTime.StopTime();
            Console.WriteLine(string.Format("共用时间:{0} ms",calcTime.Result().TotalMilliseconds));
            Console.Read();
        }        static void BuildArray(int[] arr)
        {            for (int i = 0; i < 100000; i++)
                arr[i] = 1;
        }        static void DisplayNums(int[] arr)
        {            for (int i = 0; i <= arr.GetUpperBound(0); i++)
                    Console.Write(arr[i] + " ");
        }
    }
Copy after login

Summary, Next, use C# to describe all data structures and use the CalcTiming object when counting the time consumed.
 


The above is the detailed content of Describing data structures with C# 1: Detailed code explanation of statistical code execution time objects. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template