Sponsored Ad

Thursday, March 24, 2011

How to use Stopwatch to Calculate Program Execution time in C#

The execution time is very important for a program and programmer should know the ways by which he can calculate the total program execution time. C# provide a stropwatch to calculate time. if you want to measure the time start the stopwatch before program start execution and stop the watch after program finishes the execution.

How to use Stopwatch to Calculate Program Execution time in C#

C# Code to Calculate Program Execution Time:

using System;
using System.Diagnostics;
using System.Threading;

namespace MyApp
{
    class execution_time_class
    {
        public static void Main(string[] args)
        {
           Stopwatch my_watch = new Stopwatch();

           my_watch.Start();
            for (int i = 1; i < 100; i++)
            {
                Thread.Sleep(1);
            }
            my_watch.Stop();

            Console.WriteLine("Elapsed: {0}", my_watch.Elapsed);
            Console.WriteLine("Elapsed in milliseconds: {0}", my_watch.ElapsedMilliseconds);
            Console.WriteLine("Elapsed timer ticks: {0}", my_watch.ElapsedTicks);

            Console.Read();
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates