locked
Running time of a program in C# RRS feed

  • Question

  • I have written some C# Console application which I want to test for their running times. Does the language or Visual Studio provide any such facility? What is the best way to do this?
    Friday, March 20, 2009 5:44 PM

Answers

  •  
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Diagnostics;  
     
    namespace Timing  
    {  
        public class AlgorithmTimer  
        {  
            private TimeSpan startingTime, duration;  
     
            public AlgorithmTimer()  
            {  
                startingTime = new TimeSpan(0);  
                duration = new TimeSpan(0);  
            }  
     
            public void StartTime()  
            {  
                GC.Collect();  
                GC.WaitForPendingFinalizers();  
                startingTime = Process.GetCurrentProcess().UserProcessorTime;  
            }  
     
            public void StopTime()  
            {  
                duration = Process.GetCurrentProcess().UserProcessorTime.Subtract(startingTime);  
            }  
     
            public TimeSpan Result  
            {  
                get { return duration; }  
            }  
        }  
    }  
     

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by Harry Zhu Thursday, March 26, 2009 8:37 AM
    • Marked as answer by Harry Zhu Friday, March 27, 2009 5:23 AM
    Friday, March 20, 2009 5:56 PM
  • I compiled that into a dll and here is a test run:

    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using Timing;  
     
    namespace TestingTimingClass 
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                int[] numbers = new int[9999];  
                BuildArray(numbers);  
                AlgorithmTimer at = new AlgorithmTimer();  
                at.StartTime();  
                DisplayNums(numbers);  
                at.StopTime();  
                Console.WriteLine("Time was " + at.Result.TotalSeconds + " seconds");  
                Console.ReadLine();  
     
            }  
     
            private static void BuildArray(int[] arr)  
            {              
                for (int i = 0; i < 9999; i++)  
                {  
                    arr[i] = i;  
                }  
            }  
     
            private static void DisplayNums(int[] arr)  
            {              
                for (int i = 0; i <= arr.GetUpperBound(0); i++)  
                {  
                    Console.WriteLine(arr[i]);  
                }  
            }  
        }  
    }  
     

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by Harry Zhu Thursday, March 26, 2009 8:37 AM
    • Marked as answer by Harry Zhu Friday, March 27, 2009 5:23 AM
    Friday, March 20, 2009 6:01 PM

All replies

  • You can use the Stopwatch class.
    http://blog.voidnish.com
    Friday, March 20, 2009 5:50 PM
  •  
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Diagnostics;  
     
    namespace Timing  
    {  
        public class AlgorithmTimer  
        {  
            private TimeSpan startingTime, duration;  
     
            public AlgorithmTimer()  
            {  
                startingTime = new TimeSpan(0);  
                duration = new TimeSpan(0);  
            }  
     
            public void StartTime()  
            {  
                GC.Collect();  
                GC.WaitForPendingFinalizers();  
                startingTime = Process.GetCurrentProcess().UserProcessorTime;  
            }  
     
            public void StopTime()  
            {  
                duration = Process.GetCurrentProcess().UserProcessorTime.Subtract(startingTime);  
            }  
     
            public TimeSpan Result  
            {  
                get { return duration; }  
            }  
        }  
    }  
     

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by Harry Zhu Thursday, March 26, 2009 8:37 AM
    • Marked as answer by Harry Zhu Friday, March 27, 2009 5:23 AM
    Friday, March 20, 2009 5:56 PM
  • I compiled that into a dll and here is a test run:

    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using Timing;  
     
    namespace TestingTimingClass 
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                int[] numbers = new int[9999];  
                BuildArray(numbers);  
                AlgorithmTimer at = new AlgorithmTimer();  
                at.StartTime();  
                DisplayNums(numbers);  
                at.StopTime();  
                Console.WriteLine("Time was " + at.Result.TotalSeconds + " seconds");  
                Console.ReadLine();  
     
            }  
     
            private static void BuildArray(int[] arr)  
            {              
                for (int i = 0; i < 9999; i++)  
                {  
                    arr[i] = i;  
                }  
            }  
     
            private static void DisplayNums(int[] arr)  
            {              
                for (int i = 0; i <= arr.GetUpperBound(0); i++)  
                {  
                    Console.WriteLine(arr[i]);  
                }  
            }  
        }  
    }  
     

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by Harry Zhu Thursday, March 26, 2009 8:37 AM
    • Marked as answer by Harry Zhu Friday, March 27, 2009 5:23 AM
    Friday, March 20, 2009 6:01 PM