-1

So basically what i'm trying to do is calculate the average of all the Stop Watch times that my for loop produces and out put it to the Console. i know how to take an average but i dont know how to apply it to Stopwatch times. Please help?

for (int index = 0; index < iterations; index++)
        {
            // loop to generate an array of random numbers      
            for (int count = 0; count < arrayOfInts.Length; count++)
            {
                arrayOfInts[count] = r.Next(numitems);
            }
            // a random array has been created start the clock and sort it
            Stopwatch elpased = new Stopwatch();
            elpased.Start();
            selectionSort(arrayOfInts);
            elpased.Stop();

            if (iterations == iterations) 
            {

                var average = elpased.Elapsed;

                Console.WriteLine ("Time for ___ sort to complete: " + elpased.Elapsed.ToString ());
            }
            }



            Console.ReadLine();
    }

This is what i have so far.

1 Answers1

4

I'd suggest to use ElapsedTicks instead. And you need to store the ticks for each iteration and calculate the average afterwards:

List<long> ticks = new List<long>();
for (int index = 0; index < iterations; index++)
{
    // loop to generate an array of random numbers      
    for (int count = 0; count < arrayOfInts.Length; count++)
    {
        arrayOfInts[count] = r.Next(numitems);
    }
    
    // a random array has been created start the clock and sort it
    Stopwatch elapsed = new Stopwatch();
    elapsed.Start(); 
    selectionSort(arrayOfInts);
    elpased.Stop();
    ticks.Add(elapsed.Elapsed.Ticks);
}

double avg = ticks.Average(); // create average of ticks
TimeSpan averageTimeSpan = new TimeSpan((long)avg); // cast needed from double to long

There is a little more elegant way to produce your random number array:

arrayOfInts = Enumerable.Range(0, count).Select(i => r.Next(numitems)).ToArray();

And because LINQ uses deferred execution you could even pre-declare this "query" and call ToArray() in the iteration:

List<long> ticks = new List<long>();
IEnumerable<int> randomQuery = Enumerable.Range(0, count).Select(i => r.Next(numitems));

for (int index = 0; index < iterations; index++)
{
     //creates NEW random numbers each time, because of deferred execution
     arrayOfInts = randomQuery.ToArray(); 

     ...

Another suggestion is to let the Stopwatch measure the whole time and divide the result by iterations. Stopwatches can be resumed:

IEnumerable<int> randomQuery = Enumerable.Range(0, count).Select(i => r.Next(numitems));
Stopwatch elapsed = new Stopwatch(); // only ONE instance needed
for (int index = 0; index < iterations; index++)
{
     arrayOfInts = randomQuery.ToArray();
     elapsed.Start(); // resumes without a reset
     selectionSort(arrayOfInts);
     elpased.Stop();
}
TimeSpan averageTimeSpan = new TimeSpan(elapsed.ElapsedTicks/iterations);
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Thank you very much, i made modifications to it to fix my needs. – Nnacheta Nnyagu Oct 21 '16 at 17:35
  • I was trying to do something similar but was getting numbers reported that just didn't make sense. Turns out `Elapsed.Ticks` should be used instead of `ElapsedTicks` for reasons described [here](http://geekswithblogs.net/BlackRabbitCoder/archive/2012/01/12/c.net-little-pitfalls-stopwatch-ticks-are-not-timespan-ticks.aspx). – Ocelot20 Dec 22 '17 at 17:00