using System; using System.Collections.Generic; using System.Threading; namespace MonoTests { class Program { class FibContainer { public int Value { get; set; } public void Compute(FibContainer previous, FibContainer beforePrevious) { Value = previous.Value + beforePrevious.Value; } } static void ClassTest() { long totalMem = 0; long calcTime = 0; long allocTime = 0; for (int testPass = 0; testPass < 50; testPass++) { GC.Collect(); long memStart = GC.GetTotalMemory(true); long allocStart = Environment.TickCount; FibContainer[] sequence = new FibContainer[10000]; for (int i = 0; i < sequence.Length; i++) sequence[i] = new FibContainer(); long allocEnd = Environment.TickCount; long memEnd = GC.GetTotalMemory(true); sequence[0].Value = 1; sequence[1].Value = 1; long calcStart = Environment.TickCount; for (int i = 2; i < sequence.Length; i++) sequence[i].Compute(sequence[i - 1], sequence[i - 2]); long calcEnd = Environment.TickCount; Console.WriteLine("The last Fibonacci number is " + sequence[sequence.Length - 1].Value); if (testPass == 0) continue; totalMem += (memEnd - memStart); calcTime += (calcEnd - calcStart); allocTime += (allocEnd - allocStart); } Console.WriteLine("Memory: " + totalMem); Console.WriteLine("Allocation: " + allocTime); Console.WriteLine("Calculation: " + calcTime); } struct FibContainerStruct { public int Value { get; set; } public void Compute(FibContainerStruct previous, FibContainerStruct beforePrevious) { Value = previous.Value + beforePrevious.Value; } } static void StructTest() { long totalMem = 0; long calcTime = 0; long allocTime = 0; for (int testPass = 0; testPass < 50; testPass++) { GC.Collect(); long memStart = GC.GetTotalMemory(true); long allocStart = Environment.TickCount; FibContainerStruct[] sequence = new FibContainerStruct[10000]; long allocEnd = Environment.TickCount; long memEnd = GC.GetTotalMemory(true); sequence[0].Value = 1; sequence[1].Value = 1; long calcStart = Environment.TickCount; for (int i = 2; i < sequence.Length; i++) sequence[i].Compute(sequence[i - 1], sequence[i - 2]); long calcEnd = Environment.TickCount; Console.WriteLine("The last Fibonacci number is " + sequence[sequence.Length - 1].Value); if (testPass == 0) continue; totalMem += (memEnd - memStart); calcTime += (calcEnd - calcStart); allocTime += (allocEnd - allocStart); } Console.WriteLine("Memory: " + totalMem); Console.WriteLine("Allocation: " + allocTime); Console.WriteLine("Calculation: " + calcTime); } static void SelectionSortTest() { // total time for the tests long totalTime = 0; // allocate once int[] numbers = new int[1000]; for (int testPass = 0; testPass < 10; testPass++) { // reset to decreasing order for (int i = 0; i < numbers.Length; i++) { numbers[i] = numbers.Length - i; } // note the start time long start = Environment.TickCount; // sort to increasing order for (int i = 0; i < numbers.Length; i++) { int min = numbers[i]; int minIndex = i; for (int j = i + 1; j < numbers.Length; j++) { int thisVal = numbers[j]; if (numbers[j] < min) { min = thisVal; minIndex = j; } } numbers[minIndex] = numbers[i]; numbers[i] = min; } // note the end time long end = Environment.TickCount; long elapsed = end - start; Console.WriteLine("The smallest number is " + numbers[0]); // don't tally the first pass- I'm not sure how the Mono/Dalvik caching and JIT works, but, I'll // just assume that the first pass over a method will probably incur some one time overhead costs. if (testPass > 0) totalTime += elapsed; } Console.WriteLine(totalTime); } static void ShowHelp() { Console.WriteLine("Selection Sort: ss"); Console.WriteLine("Class Test: ct"); Console.WriteLine("Struct Test: st"); } static void Main(string[] args) { if (args.Length != 1) ShowHelp(); else if (args[0] == "ss") SelectionSortTest(); else if (args[0] == "ct") ClassTest(); else if (args[0] == "st") StructTest(); else ShowHelp(); } } }