Stopwatch class in C#

CsharpProgrammingServer Side Programming

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
raja
Updated on 22-Jun-2020 14:19:14

Advertisements