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

 

posted @ 2017-11-28 12:41  以沫浅夏  阅读(2194)  评论(0编辑  收藏  举报