Recently couple of times, I have a requirement of showing execution time to the users. Both times, I followed the approach of taking a starttime variable and stop time variable and calculating difference between those variables.
Today I saw an MSDN blog, which reminds me about the class "Stopwatch", which is specifically designed for this purpose. By using this, we can efficiently find the execution time of the code and format the result with ease.
using System.Diagnostics; ....... Stopwatch objWatch = new Stopwatch(); objWatch.Start(); // put your execution code here objWatch.Stop(); TimeSpan ts = objWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Response.Write("RunTime " + elapsedTime);