-3

I made a program which is a random number generator using C# and the Xamarin platform. I have put a specific number as a target and the PC have to take randomly number and when it find the target number it gave me the number of trials to find the number. I miss only a thing which is, I want to put a timer so I can know how much time it took to find the number. And when it find it, the timer stop recording. How that can be done ?

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
Ayman
  • 23
  • 9
  • 5
    Have a look at [StopWatch](https://msdn.microsoft.com/de-de/library/system.diagnostics.stopwatch(v=vs.110).aspx). This can measure timespans. – Christoph K Mar 31 '16 at 11:44

3 Answers3

2
  1. Log the start time before your function
  2. Log the finish time after your function
  3. Output result in seconds or whatever units you want

        // 1) Log the start time before your function
        long startTime = DateTime.Now.Ticks;
    
        // 2) Log the finish time after your function
        double secondsElapsed = new TimeSpan(DateTime.Now.Ticks - startTime).TotalSeconds;
    
        // 3) Output result in seconds or whichever units you want
        Debug.WriteLine($"Function took: {secondsElapsed}");
    
Rick Penabella
  • 339
  • 2
  • 10
  • Welcome to StackOverflow! I wanted to comment on your answer. You're not wrong, but it's not the best answer. Using the `DateTime` struct just isn't precise enough for calculating operation timings. – Cameron Mar 31 '16 at 16:38
  • What is more precise then? – Rick Penabella Apr 01 '16 at 08:50
  • 2
    The `Stopwatch` class in the `System.Diagnostics` namespace is the preferred and recommended class. – Cameron Apr 04 '16 at 13:39
0

You can use the Stopwatch class to record time durations:

// Create new stopwatch.
Stopwatch stopwatch = new Stopwatch();

// Begin timing.
stopwatch.Start();

// Do something.
for (int i = 0; i < 1000; i++)
{
    Thread.Sleep(1);
}

// Stop timing.
stopwatch.Stop();

// Write result.
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • Okay I ll try it. Does this timer stop when the program found the target number ? Sorry, I am still a beginner.. – Ayman Mar 31 '16 at 11:55
  • @Ayman the `Stopwatch` will stop when you call `stopwatch.Stop()` so when you find the target number, just call `Stop()` and you'll get the expected result. – dylansturg Mar 31 '16 at 12:56
  • @Dylan S, I want it to stop automatically when it found the number cause maybe it ll take hours and hours to find it. – Ayman Mar 31 '16 at 16:01
  • @Ayman are you writing the code that finds the number? Just add another line that stops the stopwatch. Is there some reason you can't stop the stopwatch after finding the number? Is the code out of your reach? – dylansturg Mar 31 '16 at 16:03
  • @ Dylan S, I copy it and put it in my program. But it didn't work. The program have found the number but it didn't recorded the time. – Ayman Mar 31 '16 at 17:55
0

You don't need a timer for this. Just do this to get the elapsed milliseconds

var startTime = Environment.TickCount;
// do some work. 
var timeTaken =Environment.TickCount-startTime;
Daniel van Heerden
  • 836
  • 1
  • 7
  • 25