Get Execute Time Of C# Function

Introduction

Yes, this is possible. You are check execution time of any function in c sharp class through stopwatch class library, 

which is using in diagnostics namespace.

Program

Suppose we have create for loop in a program. Then we want to get execution time of running loop. See in below program:

 Example

using System;

using System.Diagnostics;     // Using Library

 

namespace get_execute_time_of_c_sharp_function

{

    class A         //create class

    {       

        int n = 5;

        int k =5;

               

        public void table()  //create function

        {           

            Console.WriteLine("Table Of 5 is:");

            var sw = Stopwatch.StartNew(); //call to startNew() function through Stopwatch class library

            for (int i = 1; i <= 10; i++) //create for loop

            {              

                n = k;

                n = n * i;

                Console.WriteLine(n);   //show table

             }

            sw.Stop();                  //call to stop() function for Stopwatch class library

            Console.Write("Looping Time is:");

            //show execute time of loop in milisecond   

            Console.WriteLine((sw.Elapsed.TotalMilliseconds).ToString("0.00 Milli Second"));

        }   

 

    }

    class Program

    {

        static void Main(string[] args)

        {           

            A obj = new A(); //create class object

            obj.table(); // call to table() function

            Console.ReadLine();

        }

    }

}

 

Output:


   output.jpg


Ebook Download
View all
Learn
View all