Stopwatch class in C#


Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System.Diagnostics.

To get the elapsed time, firstly begin the Stopwatch −

var sw = Stopwatch.StartNew();

For elapsed ticks −

long ticks = sw.ElapsedTicks;

Let us see an example −

Example

 Live Demo

using System;
using System.Linq;
using System.Diagnostics;
public class Demo {
   public static void Main() {
      var sw = Stopwatch.StartNew();
      long ticks = sw.ElapsedTicks;
      Console.WriteLine(ticks);
   }
}

Output

582

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements