程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 時間測試

時間測試

編輯:C#入門知識

[csharp] 
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] + " "); 
            } 
        } 
    } 

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類----------------------------------------------------------------------------------------------------------------------


[csharp]
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; 
        } 
    } 

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;
        }
    }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved