private static void DisplayResults(ILevenshteinFactory[] factories, TimeSpan[] times) { for (int i = 0; i < times.Length; i++) { Console.WriteLine(times[i] + "\t" + factories[i].Name); } decimal minTime = times .Select(x => x.Ticks) .Min(); for (int i = 0; i < times.Length; i++) { decimal difference = times[i].Ticks - minTime; decimal percentSlower = (difference / minTime) * 100; percentSlower = Math.Round(percentSlower, 0); Console.WriteLine(percentSlower + "%\t" + factories[i].Name); } Console.WriteLine(); }
private static void MultiThread(ILevenshteinFactory factory, string[] words, int testSize) { Parallel.For(0, testSize, i => { ILevenshtein levenshtein = factory.Create(words[i]); for (int j = 0; j < testSize; j++) { levenshtein.Distance(words[j]); } }); }
private static void SpeedTest( ILevenshteinFactory[] factories, int testSize, Action<ILevenshteinFactory, string[], int> testMethod) { Console.WriteLine("Normal Test"); Program.TestSpeed(factories, Program.NormalWords, testSize, testMethod); Console.WriteLine("Large Words Test"); Program.TestSpeed(factories, Program.LargeWords, testSize / 18, testMethod); Console.WriteLine("Small Words Test"); Program.TestSpeed(factories, Program.SmallWords, testSize * 3, testMethod); }
private static void TestSpeed( ILevenshteinFactory[] factories, string[] words, int testSize, Action<ILevenshteinFactory, string[], int> testMethod) { TimeSpan[] times = new TimeSpan[factories.Length]; for (int i = 0; i < factories.Length; i++) { TimeSpan timeTaken = TimeSpan.MaxValue; // do the test 5 times and take the min value to rule out any anomalies for (int numberOfTimes = 0; numberOfTimes < 3; numberOfTimes++) { Stopwatch stopwatch = Stopwatch.StartNew(); testMethod(factories[i], words, testSize); stopwatch.Stop(); // min timeTaken = timeTaken < stopwatch.Elapsed ? timeTaken : stopwatch.Elapsed; } times[i] = timeTaken; } Program.DisplayResults(factories, times); }
private static void SingleThread(ILevenshteinFactory factory, string[] words, int testSize) { for (int i = 0; i < testSize; i++) { ILevenshtein levenshtein = factory.Create(words[i]); for (int j = 0; j < testSize; j++) { levenshtein.Distance(words[j]); } } }