データ構造とアルゴリズム–番外編.時間テストクラスTiming

4609 ワード

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