Calculate the execution time of a method

后端 未结 7 930
小蘑菇
小蘑菇 2020-11-22 13:26

Possible Duplicate:
How do I measure how long a function is running?

I have an I/O time-taking method which c

7条回答
  •  甜味超标
    2020-11-22 14:23

    Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET.

    var watch = System.Diagnostics.Stopwatch.StartNew();
    // the code that you want to measure comes here
    watch.Stop();
    var elapsedMs = watch.ElapsedMilliseconds;
    

    Do not use DateTime to measure time execution in .NET.


    UPDATE:

    As pointed out by @series0ne in the comments section: If you want a real precise measurement of the execution of some code, you will have to use the performance counters that's built into the operating system. The following answer contains a nice overview.

提交回复
热议问题