简体   繁体   中英

How to calculate the average of multiple Stopwatch times?

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.

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 . Stopwatch es 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM