How to use Stopwatch class to time recursive function iterations by a factor of 3.

  • Thread starter Thread starter StudiousStudent
  • Start date Start date
S

StudiousStudent

Guest
I'm trying to time the recursion of my code using the Stopwatch class. I can time the entire Merge Sort process which you can see below, however, I want to show run times for the merge sort where the size of the input data increases by a factor of 3 each time. Ie where the input size is 9 random data values are generated in the range 1 to 9 and where the input size is 27 the data range is 1 to 27. I'm not sure how to do this. The array size should increase by a factor of 3 on each iteration ie n = 1, 3, 9, 27 177147. Any help with this problem would be appreciated.



List<int> unsorted = new List<int>();
List<int> sorted = new List<int>();
Random random = new Random();
Stopwatch stopwatch = new Stopwatch();

int[] values = { 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147 };

for (int i = 0; i < 200000; i++)
{
unsorted.Add(random.Next(0, 40 + 1));
}

// This times the ammount of time it takes to complete the merge sort in it's entirety.
stopwatch.Start();
sorted = MergeSort(ref unsorted);
stopwatch.Stop();

TimeSpan ts = stopwatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime: {0} ", elapsedTime);

Console.ReadLine();





StudiousStudent

Continue reading...
 
Back
Top