Ask a questionAsk a question
 

Answerinstructions excution time

  • Wednesday, November 04, 2009 11:53 PMENGmahrous Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    can i get to know how much time exactly some set of instructions take to execute ?
    thanks in advance :)

Answers

  • Thursday, November 05, 2009 12:09 AMYort Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.

All Replies

  • Thursday, November 05, 2009 12:09 AMYort Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Thursday, November 05, 2009 12:10 AMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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