Exact Time Measurement for Performance Testing in C#

Muhammad Zeeshan Oct 12, 2023
  1. Overview of Stopwatch
  2. Implement Stopwatch for Exact Time Measurement for Performance Testing in C#
Exact Time Measurement for Performance Testing in C#

In this article, you will learn how to measure the exact time for performance testing using C#. The System.Diagnostics namespace has an excellent class known as Stopwatch that provides a straightforward method for measuring the amount of time an execution takes.

Let’s have an overview of Stopwatch.

Overview of Stopwatch

The Stopwatch built-in class was made available in the .Net framework and can be used in various situations. It assists you in optimizing your code, selecting the best option from several potential solutions, or locating a performance spike.

In other words, a Stopwatch is a simple device that can determine how long a particular process or activity takes to complete. In circumstances where the amount of time plays a crucial role, it is helpful to optimize the code.

It has a high level of accuracy and comes in handy when other monitoring tools with a higher level of complexity are either unavailable or not required for the sake of simplicity.

Implement Stopwatch for Exact Time Measurement for Performance Testing in C#

Let’s have a look at the basic implementation of Stopwatch. The following characteristics can be used to determine the amount of time that has passed:

  1. .Elapsed
  2. .ElapsedMilliseconds
  3. .ElapsedTicks

In the below demonstration, we’ve implemented the .Elapsed function, which will output the process elapsed time.

Example

  • To utilize the Stopwatch utility, import the following libraries:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    
  • In the Main() method, create Stopwatch object named s.
    Stopwatch s = new Stopwatch();
    
  • Start the measurement of the process:
    s.Start();
    
  • Write your process code now. For example, we used a basic for() loop for printing a string value.
    for (int i = 0; i < 5; i++) {
      Console.WriteLine("Myself M.Zeeshan, Let's Code!");
    }
    
  • After writing your code, stop the measurement of the process.
    s.Stop();
    
  • Lastly, we’ll use the .Elapsed function to check the process time.
    Console.WriteLine("Time elapsed: {0}", s.Elapsed);
    

Complete Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace StopwatchbyZeeshan {
  class Program {
    static void Main(string[] args) {
      Stopwatch s = new Stopwatch();
      s.Start();
      for (int i = 0; i < 5; i++) {
        Console.WriteLine("Myself M.Zeeshan, Let's Code!");
      }
      s.Stop();
      Console.WriteLine("Total Time elapsed: {0}", s.Elapsed);
    }
  }
}

Output:

Myself M.Zeeshan, Let's Code!
Myself M.Zeeshan, Let's Code!
Myself M.Zeeshan, Let's Code!
Myself M.Zeeshan, Let's Code!
Myself M.Zeeshan, Let's Code!
Total Time elapsed: 00:00:00.0144367
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn