instructions excution time
- can i get to know how much time exactly some set of instructions take to execute ?thanks in advance :)
Answers
- Hi,
Use the System.Diagnostics.Stopwatch class, i.e something like
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
// your instructionrs here
stopWatch.Stop();
then you can refer to either
stopWatch.ElapsedTicks, stopWatch.ElapsedMilliseconds, or stopWatch.Elapsed which returns a TimeSpan object that will break the time taken down in to seconds/minutes etc.
Alternatively, if you have the right sku of VS (not sure if you need Pro, Developer or Team Suite) there is a source code profiler built in which can be used from the 'Anaylze' menu of VS... if you have it. There are also some free/cheap profiler... Red Gate Software's one seems to be popular.- Proposed As Answer byChao KuoMSFT, ModeratorFriday, November 06, 2009 7:29 AM
- Marked As Answer byChao KuoMSFT, ModeratorTuesday, November 10, 2009 7:48 AM
All Replies
- Hi,
Use the System.Diagnostics.Stopwatch class, i.e something like
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
// your instructionrs here
stopWatch.Stop();
then you can refer to either
stopWatch.ElapsedTicks, stopWatch.ElapsedMilliseconds, or stopWatch.Elapsed which returns a TimeSpan object that will break the time taken down in to seconds/minutes etc.
Alternatively, if you have the right sku of VS (not sure if you need Pro, Developer or Team Suite) there is a source code profiler built in which can be used from the 'Anaylze' menu of VS... if you have it. There are also some free/cheap profiler... Red Gate Software's one seems to be popular.- Proposed As Answer byChao KuoMSFT, ModeratorFriday, November 06, 2009 7:29 AM
- Marked As Answer byChao KuoMSFT, ModeratorTuesday, November 10, 2009 7:48 AM
- There are two ways to get an approximation of the amount of time it takes to execute a block of code.
The most common means of doing this is to use a Code Profiler. There are many good commercial profilers for C#, and a few free ones (though somewhat less feature rich).
Alternatively, you can use a System.Diagnostics.Stopwatch to time the lines of code/method call in question yourself. The stopwatch MSDN page has sample code demonstrating its use.
Reed Copsey, Jr. - http://reedcopsey.com


