C# – Stopwatch

By | 05/04/2023

In this post, we will see what StopWatch is and how we can use it.
But first of all, what is StopWatch?
The Stopwatch class is a high-resolution timer provided by the .NET Framework, designed to measure the elapsed time for an operation or a block of code. It’s part of the System.Diagnostics namespace and offers greater precision and accuracy than other timer classes, like DateTime, which can be affected by system clock changes.
Stopwatch measures elapsed time using the system’s high-resolution performance counter, which provides high-precision and high-accuracy timing data. The Stopwatch class is useful for performance analysis, profiling, and benchmarking code, as it allows you to measure the time taken for an operation with great accuracy.
For all information, we can look up the Microsoft web site.

Let’s take a look at some examples.
First, open Visual Studio, create a Console Application, and begin modifying the Program.cs file as follows:

[PROGRAM.CS]

using System.Diagnostics;

Console.WriteLine("Testing StopWatch");

// We create an instance of Stopwatch
Stopwatch stopwatch = new Stopwatch();   

// We start the stopwatch
stopwatch.Start();

// We call the method Func1
Func1();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func1
Console.WriteLine($"The time elapsed for Func1 is: {stopwatch.Elapsed.TotalSeconds}");

void Func1()
{
    // It simulates a real workflow
    Thread.Sleep(1500);
}


If we run the application, the following will be the result:


If we want to check two or more methods, we have just to reset the stopwatch instance and start it again:

[PROGRAM.CS]

using System.Diagnostics;

Console.WriteLine("Testing StopWatch");

// We create an instance of Stopwatch
Stopwatch stopwatch = new Stopwatch();   

// We start the stopwatch
stopwatch.Start();

// We call the method Func1
Func1();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func1
Console.WriteLine($"The time elapsed for Func1 is: {stopwatch.Elapsed.TotalSeconds}");


// We reset the stopwatch
stopwatch.Reset();  

// We start the stopwatch again
stopwatch.Start();

// We call the method Func2
Func2();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func2
Console.WriteLine($"The time elapsed for Func2 is: {stopwatch.Elapsed.TotalSeconds}");


void Func1()
{
    // It simulates a real workflow
    Thread.Sleep(1500);
}

void Func2()
{
    // It simulates a real workflow
    Thread.Sleep(1000);
}


If we run the application, the following will be the result:


It is important to highlight that the row number 22 is very important because, if we don’t reset the Stopwatch, the second time we will have the entire elapsed for running Func1 and Func2.
In fact, if we comment the row 22 and then we run the application, the following will be the result:


If we don’t want to use the Reset() method, we can use the StartNew() method, which creates a new Stopwatch instance and starts it:

[PROGRAM.CS]

using System.Diagnostics;

Console.WriteLine("Testing StopWatch");

// We create an instance of Stopwatch
Stopwatch stopwatch = new Stopwatch();   

// We start the stopwatch
stopwatch.Start();

// We call the method Func1
Func1();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func1
Console.WriteLine($"The time elapsed for Func1 is: {stopwatch.Elapsed.TotalSeconds}");


// StartNew() method creates a new instance of Stopwatch and starts it
stopwatch = Stopwatch.StartNew();  

// We call the method Func2
Func2();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func2
Console.WriteLine($"The time elapsed for Func2 is: {stopwatch.Elapsed.TotalSeconds}");


void Func1()
{
    // It simulates a real workflow
    Thread.Sleep(1500);
}

void Func2()
{
    // It simulates a real workflow
    Thread.Sleep(1000);
}


If we run the application, the following will be the result:


Of course, if we want to check three or more methods, we need to reset the Stopwatch for each method:

[PROGRAM.CS]

using System.Diagnostics;

Console.WriteLine("Testing StopWatch");

// We create an instance of Stopwatch
Stopwatch stopwatch = new Stopwatch();   

// We start the stopwatch
stopwatch.Start();

// We call the method Func1
Func1();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func1
Console.WriteLine($"The time elapsed for Func1 is: {stopwatch.Elapsed.TotalSeconds}");


// We can use StartNew() to create a new instance of Stopwatch and starting it
stopwatch = Stopwatch.StartNew();  

// We call the method Func2
Func2();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func2
Console.WriteLine($"The time elapsed for Func2 is: {stopwatch.Elapsed.TotalSeconds}");


// We can use StartNew() to create a new instance of Stopwatch and starting it
stopwatch = Stopwatch.StartNew();

// We call the method Func3
Func3();

// We stop the stopwatch
stopwatch.Stop();

// We print in the video the elapsed time for running the Func3
Console.WriteLine($"The time elapsed for Func3 is: {stopwatch.Elapsed.TotalSeconds}");


void Func1()
{
    // It simulates a real workflow
    Thread.Sleep(1500);
}

void Func2()
{
    // It simulates a real workflow
    Thread.Sleep(1000);
}

void Func3()
{
    // It simulates a real workflow
    Thread.Sleep(2000);
}


If we run the application, the following will be the result:



Category: C# Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *