Back To FAQ

How to calculate code execution time in C#?

As a programmer, we may need to find out the execution time taken by a particular segment of C# code in order to optimize the performance. For example, how much time taken in reading multiple files in file system or fetching data from the database or executing some business logic.

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

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

Example: Stopwatch
var watch = new System.Diagnostics.Stopwatch();
            
watch.Start();

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

watch.Stop();

Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
Output:
Execution Time: 100 ms

In the above example, first it creates an instance of the Stopwatch class. The Start() method starts measuring time to 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 time taken in executing the for loop. (Please note that output showed is arbitrary.)

Use the StartNew method to initialize an instance of the Stopwatch and starts the counter immediately without creating an instance of the Stopwatch using the new keyword.

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

watch.Stop();

Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
Output:
Execution Time: 100 ms

The following example measures the total time taken in executing different code segments.

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

watch.Stop();
            
// some code here..


if (!watch.IsRunning) // checks if it is not running
    watch.Start(); // Start the counter from where it stopped
            
for (int j = 0; j < 100; j++)
{
    Console.Write(j);
}

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

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

The following example measures the time taken in executing each code segments.

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

watch.Stop();

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

// some code here..

if (!watch.IsRunning)
    watch.Restart(); // Reset time to 0 and start measuring
            
for (int j = 0; j < 100; j++)
{
    Console.Write(j);
}

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

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

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

Visit Stopwatch on MSDN for more information.