How much time does a method take to run?

I frequently run into the situation where I quickly want to learn how fast one method or section of code runs in my unit testing.  Here’s a quick and dirty way to get it done:

 
long startTime = DateTime.Now.Ticks;
 
ExecuteMyMethod();
 
long endTime = DateTime.Now.Ticks;
 
TimeSpan timeTaken = new TimeSpan(endTime - startTime);
Console.WriteLine("execution time: " + timeTaken.ToString());

Hope this helps!

This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *