Measure execution time in C#

前端 未结 8 618
广开言路
广开言路 2020-12-13 18:12

I want to measure the execution of a piece of code and I\'m wondering what the best method to do this is?

Option 1:

DateTime StartTime = DateTime.Now         


        
8条回答
  •  一生所求
    2020-12-13 18:31

    I have a little class to do this sort of thing ad hoc. It uses the stopwatch class - c# micro perfomance testing.

    eg.

    var tester = new PerformanceTester(() => SomeMethod());
    tester.MeasureExecTime(1000);
    Console.Writeline(string.Format("Executed in {0} milliseconds", tester.AverageTime.TotalMilliseconds));
    

提交回复
热议问题