c# 測試方法執行時間


    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(ActionExtension.Profiler(a, 10));
        }

        static void a()
        { }
    }

    //public class test : ICorProfilerCallback
    //{
    //}

  public  static class ActionExtension
    {
        public static string Profiler(this Action func, int runcount)
        {
            Stopwatch watch = Stopwatch.StartNew();//創建一個監聽器
            for (int i = 0; i < runcount; i++)
            {
                func();//執行某個方法
            }
            watch.Stop();

            float sec = watch.ElapsedMilliseconds / 1000.0f;
            float freq = sec / runcount;

            return string.Format("總體執行時間為:{0}秒,總體執行次數為:{1},平均執行時間為:{2}秒", sec, runcount, freq);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM