C# how to measure elapsed time?

2.6k views Asked by At

I want to record the amount of time it takes to execute my custom method, but I have never worked with the timer class or methods before, I know I'm doing something wrong, .

Here's what I have.

System.Timers.Timer Time;
int Mili = 0 ;

data = Generate();

Time.Enabled = true;
BSort= BubbleSort(data);

Time.Enabled = false;
1

There are 1 answers

0
Daniel A. White On

A timer is for a periodic event. You should look at the Stopwatch class.

var sw = Stopwatch.StartNew();

// your code ...

// sw.Stop(), sw.Elapsed, etc.