Bas
Bas

Reputation: 477

Tool to measure the time taken for a code to run in visual studio 2010 ide

Is there any tool that I can use to measure the time take for a function or a part of code to execute in visual studio 2010.

Thanks for your help

Upvotes: 0

Views: 89

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

You dont need a tool rather you can use the System.Diagnostics.Stopwatch.

Provides a set of methods and properties that you can use to accurately measure elapsed time.

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time.

Example:

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value. 
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);
    }
}

Upvotes: 2

Related Questions