Open In App

How to Calculate the Code Execution Time in C#?

Last Updated : 23 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The execution time of a task is defined as the time taken by the system to execute that task. The execution time of a program is equal to the sum of the execution time of its statements. There are numerous ways to calculate the execution time of a program in C#. In this article, we will see three ways to measure execution time in C#.

Method 1: Using StartNew() and Stop() method 

We can calculate the execution time of the code using StartNew() and Stop() methods. StartNew() method comes under the Stopwatch class and it basically used to initialize a new Stopwatch instance. Internally it marks the elapsed time property equal to zero and then it starts measuring the elapsed time. This method is almost the same as calling the Stopwatch constructor first and then calling class on the new instance. 

Syntax:

public static StartNew ();

Stop() method also comes under the Stopwatch class and it is used to stop the current measuring time for an interval. Firstly we can call StartNew() method in Stopwatch to start the elapsed time and at the end, we can call this method to end the current measuring time interval. Eventually, the total execution time is calculated with the help of elapsed property. Whenever a Stopwatch instance measures more than one interval, this method is similar to pausing the elapsed time measurement. Like StartNew() method, this method is also defined under System.Diagnostics namespace. 

Syntax:

public void Stop ();

Example:

C#




// C# program to find the execution time of the code
using System;
using System.Diagnostics;
  
class GFG{
  
static public void Main()
{
      
      // Starting the Stopwatch
    var watch = Stopwatch.StartNew();
      
      // Iterating using for loop
    for(int i = 0; i < 5; i++) 
    {
          
        // Print on console
        Console.WriteLine("GeeksforGeeks");
    }
      
      // Stop the Stopwatch
    watch.Stop();    
      
      // Print the execution time in milliseconds
      // by using the property elapsed milliseconds
      Console.WriteLine(
          $"The Execution time of the program is {watch.ElapsedMilliseconds}ms");
}
}


Output

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
The Execution time of the program is 37ms

Method 2: Using the GetTimestamp() method 

We can also find the execution time of the code using the GetTimestamp() method. This method is quite helpful for finding the number of ticks for a duration in the timer mechanism. We can use the high-resolution performance counter of the Stopwatch class to get the current value of that counter. We can use the system timer also to get the current DateTime.Ticks property of the DateTime.UtcNow instance. Its return type is a long integer that represents the tick counter value of the timer mechanism.

Syntax:

public static long GetTimestamp ();

Example:

C#




// C# program to find the execution time of the code
using System;
using System.Diagnostics;
  
class GFG{
  
static public void Main()
{
      
      // Mark the start before the loop
      var start = Stopwatch.GetTimestamp();
         
      // Iterating using for loop
    for(int i = 0; i < 5; i++) 
    {
          
        // Print on console
        Console.WriteLine("GeeksforGeeks");
    }
      
      // Mark the end after the loop
    var end = Stopwatch.GetTimestamp();
   
      // Print the difference
    Console.WriteLine("Elapsed Time is {0} ticks", (end - start));
}
}


Output

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Elapsed Time is 343095 ticks

Method 3: Using DateTime.Now property

We can calculate the execution time of the code using the DateTime.Now property. This property is quite helpful to get a DateTime object that is initially marked with the current date and time on the device (as the local time). The property value of this method is DateTime which is an object whose value is current local data and time. The Now property returns a DateTime value which depicts the current date and time on the device or computer. This is defined under the System namespace.

Syntax:

public static DateTime Now { get; }

Example:

C#




// C# program to find the execution time of the code
using System;
  
class GFG{
  
static public void Main()
{
      
      // Marking the start time
      DateTime start = DateTime.Now;
        
      // Iterating using for loop
       for(int i = 0 ; i < 5 ; i++)
    {
        Console.WriteLine("GeeksforGeeks");    
    }
  
      // Marking the end time
    DateTime end = DateTime.Now;
       
      // Total Duration 
    TimeSpan ts = (end - start);
        
      // Print the execution time in milliseconds
    Console.WriteLine("The execution time of the program is {0} ms"
                      ts.TotalMilliseconds);
}
}


Output:

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
The execution time of the program is 176.095 ms


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads