1

I've got a process that fills in a random board, hopefully in the most efficient way, I've got a loop that I'm running that I want to time from the start of the loop to when it finishes like a stopwatch so I can then compare the result from one path finding algorithm to another, I've not used timers like this before and am unsure if I'm going about it the wrong way.

This, I hope shows more what I'm trying to do

 System.Timers.Timer gameTime = new System.Timers.Timer();

 gameTime.Interval = (1000);
 gameTime.Enabled = true;
 gameTime.Start();            

 m_turn = 1;

do
{
    //Proccess here
    m_turn++

while(m_turn <= 1000)

gameTime.Stop();
string printTime = gameTime. //The amount of times the interval has passed?
Wizard
  • 1,142
  • 3
  • 16
  • 36

1 Answers1

7

Since you want to measure time interval I'd use StopWatch instead of a Timer:

var sw = System.Diagnostics.StopWatch.StartNew();
PerformSecretAlgorithm();
sw.Stop();
long elapsed = sw.ElapsedMilliseconds();
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
  • `Timer`s have their own uses, it's better to use `StopWatch` when you want to measure intervals. The thing is that you have to know that there is a something like that already available. – Zbigniew Dec 07 '13 at 10:47