Sunday, 12 April 2020

Best way to Find the Execution time of your code - C#Revisited


You want to find out exactly where it takes time? Believe it or not it is the most difficult point when it comes to the code you have written, unless it is not from Stack Overflow, which is very rare among us.

I have this simple solution to analyze the code based on the execution time of each section of code, thanks to such great features I get in c#.

C# includes the Stopwatch class in the System.Diagnostics namespace, which can be used to accurately measure the time taken for code execution. You don't need to use DateTime and calculate the time manually.

The following example measures the time taken for the execution of the for loop using StopWatch.

Example: Stopwatch

var execTime = new System.Diagnostics.Stopwatch();
           
execTime.Start();

for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();

Console.WriteLine($"Execution Time: {execTime.ElapsedMilliseconds} ms");

Output:
Execution Time: 100 ms

In the above example, first we create an instance of the Stopwatch class. The Start() method starts measuring time for executing the code until we call the Stop() method. The ElapsedMilliseconds property gets the total time measured by the current instance in milliseconds. Here, it will return the time taken in executing the for loop. (Please note that output showed is arbitrary.)

You can use the StartNew method to initialize an instance of Stopwatch. Also, you can start the counter immediately without creating an instance of Stopwatch using the new keyword.

Example: Stopwatch.StartNew()

var execTime = System.Diagnostics.Stopwatch.StartNew();
           
for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();

Console.WriteLine($"Execution Time: { execTime.ElapsedMilliseconds} ms");

Output:
Execution Time: 100 ms

The following example measures the total time taken for executing different code segments.
var execTime = System.Diagnostics.Stopwatch.StartNew();
           
for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();
           

if (!execTime.IsRunning)
    execTime.Start();
           
for (int j = 0; j < 100; j++)
{
    Console.Write(j);
}

execTime.Stop();
Console.WriteLine($"Total Execution Time: {execTime.ElapsedMilliseconds} ms");

Output:
Total Execution Time: 130 ms

In the above example, the IsRunning() method checks whether a stopwatch is stopped or not (checks if the Stop() method has been called or not). If it is true then the Stop() method has not been called and the stopwatch is still running. If it is false, then the stopwatch is stopped. If a stopwatch is not running, then we can restart it and it will continue measuring time from where it stopped. Thus, we can measure the total execution time taken by different code segments.

The following example measures the time taken for executing each code segment.

var execTime = System.Diagnostics.Stopwatch.StartNew();
           
for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();

Console.WriteLine($"Loop 1 Execution Time: {execTime.ElapsedMilliseconds} ms");


if (!execTime.IsRunning)
    execTime.Restart();
           
for (int j = 0; j < 100; j++)
{
    Console.Write(j);
}

execTime.Stop();
Console.WriteLine($"Loop 2 Execution Time: {execTime.ElapsedMilliseconds} ms");

Output:

Loop 1 Execution Time: 100 ms
Loop 2 Execution Time: 30 ms

In the above example, the Restart() method resets time to zero and starts measuring again using the same instance of Stopwatch. In this way, we can measure the execution time of different code segments using the same instance. We don't need to create a separate instance for each code segment.


No comments:

Post a Comment