I have method for computing Levenshtein distance
public static LevenshteinMatches LevenshteinSingleThread(this string str, string expression, int maxDistance) { if (str.Length > expression.Length + 1) { int len = expression.Length; long strLen = str.Length - len + 1; int[] results = new int[strLen]; int[][,] dimension = new int[strLen][,]; for (int i = 0; i < strLen; i++) { dimension[i] = new int[len + 1, len + 1]; } string source = str; source = source.ToUpper(); expression = expression.ToUpper(); for (int i = 0; i < strLen; i++) { results[i] = SqueareLevenshtein(ref dimension[i], str.Substring(i, len).ToUpper(), expression, len); } LevenshteinMatches matches = new LevenshteinMatches(); for (int i = 0; i < strLen; i++) { if (results[i] <= maxDistance) { matches.addMatch(str.Substring(i, len), Math.Round((1.0 - ((double)results[i] / len)) * 100.0, 2), i, len, results[i]); } } return matches; } else { LevenshteinMatch match = str.LevenshteinCPU(expression, maxDistance); if (match != null) return new LevenshteinMatches(match); else return new LevenshteinMatches(); } } What should I do to make it async?
Or should i leave this method and just call it some different way?
Here is my attempt to make it async; Not sure what is wrong but I can't get any results - thread is inf working but it should take only few ms.
public static async Task<LevenshteinMatches> LevenshteinSingleThread(this string str, string expression, int maxDistance) { return await Task.Factory.StartNew(() => { if (str.Length > expression.Length + 1) { int len = expression.Length; long strLen = str.Length - len + 1; int[] results = new int[strLen]; int[][,] dimension = new int[strLen][,]; for (int i = 0; i < strLen; i++) { dimension[i] = new int[len + 1, len + 1]; } string source = str; source = source.ToUpper(); expression = expression.ToUpper(); for (int i = 0; i < strLen; i++) { results[i] = SqueareLevenshtein(ref dimension[i], str.Substring(i, len).ToUpper(), expression, len); } LevenshteinMatches matches = new LevenshteinMatches(); for (int i = 0; i < strLen; i++) { if (results[i] <= maxDistance) { matches.addMatch(str.Substring(i, len), Math.Round((1.0 - ((double)results[i] / len)) * 100.0, 2), i, len, results[i]); } } return matches; } else { LevenshteinMatch match = str.LevenshteinCPU(expression, maxDistance); if (match != null) return new LevenshteinMatches(match); else return new LevenshteinMatches(); } }); } rest of the code link
And that's how I call it:
string s = "xcjavxzcbvmrmummuuutmtumuumtryumtryumtrutryumtryumtrymutryumtyumtryumtrmutyumtrurtmutymurtmyutrymut"; s = string.Concat(Enumerable.Repeat(s, 4000)); var watch = System.Diagnostics.Stopwatch.StartNew(); var ret = s.LevenshteinSingleThread("jas", 1); var res = ret.Result; watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds;
await retinstead of.Result.