How to find method execution time in c#

In some time i wanted to measure method or the loop execution time in c#. I found that using c# Stopwatch class we can measure it. Here i have provided a simple example. Measuring code execution time can be used to optimize the code.

Here is the utility class which contain methods of calculating.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Elapsed_Time
{
    /// <summary>
    /// Source URL: http://stackoverflow.com/questions/2329079/how-do-you-convert-stopwatch-ticks-to-nanoseconds-milliseconds-and-seconds
    /// </summary>
    public static class Utility
    {
        public static long ElapsedNanoSeconds(this Stopwatch watch)
        {
            return watch.ElapsedTicks * 1000000000 / Stopwatch.Frequency;
        }
        public static long ElapsedMicroSeconds(this Stopwatch watch)
        {
            return watch.ElapsedTicks * 1000000 / Stopwatch.Frequency;
        }
        public static long ElapsedMiliseconds(this Stopwatch watch)
        {
            return watch.Elapsed.Milliseconds;
        }
        public static long ElapsedSeconds(this Stopwatch watch)
        {
            return watch.Elapsed.Seconds;
        }
        public static long ElapsedMinutes(this Stopwatch watch)
        {
            return watch.Elapsed.Minutes;
        }
        public static long ElapsedHours(this Stopwatch watch)
        {
            return watch.Elapsed.Hours;
        }
    }
}

and i have added another simple class which contain my looping methods and this class looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Elapsed_Time
{
    public class Common
    {
        public void runWithForLoop(List<int> values)
        {
            for (int i = 0; i < values.Capacity; i++)
            {

            }
        }
        public void runWithForEachLoop(List<int> values)
        {
            foreach (int i in values)
            {

            }
        }
    }
}

and my main class is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace Elapsed_Time
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch objStopWatch = new Stopwatch();
            Common c = new Common();
            List<int> values = new List<int>();

            for (int i = 0; i < 1000; i++)
            {
                values.Add(i);
            }

            objStopWatch.Start();
            c.runWithForLoop(values);
            objStopWatch.Stop();
            Console.WriteLine("Time elapsed for loop \n{0}\tHours \n{1}\tMinutes \n{2}\tSeconds \n{3}\tMiliseconds \n{4}\tMicroseconds \n{5}\tNanoseconds", Utility.ElapsedHours(objStopWatch), Utility.ElapsedMinutes(objStopWatch), Utility.ElapsedSeconds(objStopWatch), Utility.ElapsedMiliseconds(objStopWatch), Utility.ElapsedMicroSeconds(objStopWatch), Utility.ElapsedNanoSeconds(objStopWatch));

            objStopWatch.Restart();
            Console.WriteLine();
            objStopWatch.Start();
            c.runWithForEachLoop(values);
            objStopWatch.Stop();

            Console.WriteLine("Time elapsed foreach loop \n{0}\tHours \n{1}\tMinutes \n{2}\tSeconds \n{3}\tMiliseconds \n{4}\tMicroseconds \n{5}\tNanoseconds", Utility.ElapsedHours(objStopWatch), Utility.ElapsedMinutes(objStopWatch), Utility.ElapsedSeconds(objStopWatch), Utility.ElapsedMiliseconds(objStopWatch), Utility.ElapsedMicroSeconds(objStopWatch), Utility.ElapsedNanoSeconds(objStopWatch));

        }

    }
}

and the final output is

method_execution_time_32

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s