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