How to Calculate the Code Execution Time in C#?
Last Updated :
23 Nov, 2021
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#
using System;
using System.Diagnostics;
class GFG{
static public void Main()
{
var watch = Stopwatch.StartNew();
for ( int i = 0; i < 5; i++)
{
Console.WriteLine( "GeeksforGeeks" );
}
watch.Stop();
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#
using System;
using System.Diagnostics;
class GFG{
static public void Main()
{
var start = Stopwatch.GetTimestamp();
for ( int i = 0; i < 5; i++)
{
Console.WriteLine( "GeeksforGeeks" );
}
var end = Stopwatch.GetTimestamp();
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#
using System;
class GFG{
static public void Main()
{
DateTime start = DateTime.Now;
for ( int i = 0 ; i < 5 ; i++)
{
Console.WriteLine( "GeeksforGeeks" );
}
DateTime end = DateTime.Now;
TimeSpan ts = (end - start);
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
Share your thoughts in the comments
Please Login to comment...