Calculate the execution time of a method

I found a simple code to know the execution time of a method. First of all you have to import the following namespace:

using System.Diagnostics;

Then you use Stopwatch to know the execution time.

Stopwatch sw = Stopwatch.StartNew();
DoSomeWork();
sw.Stop();

Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

 

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.