.NET provides plenty of ways for delaying the execution using timers. But many programmers keep thinking of getting a way out to know the time required to execute a code in the application. How do we do this?
The conventional way to do this is to do something like the following snippet:
- int startTime = System.DateTime.Now.Millisecond;
- //Code to be evaluated
- int stopTime = System.DateTime.Now.Millisecond;
- int difference = stopTime - startTime;
The difference in time is available as a TimeSpan and is formatted as d.hh:mm:ss.ff:
- d = days
- hh = hours
- mm = minutes
- ss = seconds
- ff = fractions of a second
The following is an example of using StopWatch Class to get the time elapsed instead of the above method.
- using System.Diagnostics; //namespace
- Stopwatch stopWatchObj = new Stopwatch();
- stopWatchObj.Start();
- // Code to be evaluated
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
Happy Coding.

SubashPosted Feb 26, 2017, 1:22 AM
Very basic things are always interesting to know thank mam
Apurba RanjanPosted Jul 25, 2016, 4:02 AM
Thanks... Good One
Kartik VaghasiyaPosted Sep 9, 2015, 8:06 AM
nice code.
Santhakumar MunuswamyPosted Sep 7, 2015, 2:34 PM
Thanks for nice share