Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Get time of Code Execution Using StopWatch

, 25 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
During Development of the application/product or after deployment of the application/product there might be a situation where you want to find out the the how much time is taken by you code to execute ? Is it too much slow ?Answer to this problem is make use of StopWatch class of System.Diagnostics
During Development of the application/product or after deployment of the application/product there might be a situation where you want to find out the the how much time is taken by you code to execute ? Is it too much slow ?

Answer to this problem is make use of StopWatch class of System.Diagnostics namespace which is usefull to find out the time taken to execute given line of code. The class is helpfull to find out how efficient code develop by measuring the time of execution.

To understand how to use it consider the below demo
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
//instead of this there is line of code that you are going to execute
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine(elapsedTime);
Console.ReadLine();
Stopwatch class has method Start() and Stop(), So the as name suggest call start when you want to start your watch and call Stop when you want to stop the watch. Once you stop the watch i.e called stop method of the StopWatch class you can get the value of time of execution by using Elapsed property of the class which return TimeSpan object.

Output



There are also other important methods which are very usefull you can get the more infomation about those on the MSDN documentation over here : StopWatch Class
Methods
StartNew - Initializes a new Stopwatch instance, sets the elapsed time property to zero, and starts measuring elapsed time.
Restart - Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.
Reset - Stops time interval measurement and resets the elapsed time to zero.

Properties
ElapsedMilliseconds - Gets the total elapsed time measured by the current instance, in milliseconds.
ElapsedTicks - Gets the total elapsed time measured by the current instance, in timer ticks.

Advantage
  • Easy and Simple to use.
  • Useful when want to find out the time take by the line of code to execute.
  • By using the class there is no need of any third party tool because it part of the .net framework.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionA way to measure in a "using" PinmemberMaxim Novak1-Aug-11 19:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 25 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid