Skip to main content
added 308 characters in body
Source Link
Scott Chamberlain
  • 128k
  • 37
  • 299
  • 448

There is nothing async about your function, it is entirely CPU bound work. Changing the signature to async Task<LevenshteinMatches> and never using await in the function should have caused a warning in the compiler.

Instead of "making it async" if the thing you are really after is making it work in parallel then just call the code in parallel.

string s = "xcjavxzcbvmrmummuuutmtumuumtryumtryumtrutryumtryumtrymutryumtyumtryumtrmutyumtrurtmutymurtmyutrymut"; s = string.Concat(Enumerable.Repeat(s, 4000)); var expressions = new[] {"jas", "cbv"} var tasks = new List<Task<LevenshteinMatches>>() foreach(var expression in expressions) { var task = new Task.Run(()=> message.LevenshteinSingleThread(expression, 1)); //Start multiple threads tasks.Add(task); } LevenshteinMatches[] results = Task.WaitAll(tasks); //Wait for all the threads to end. 

You could even make parts of the internal function multi-threaded by using Parallel.For( to make some of the for loops parallel, just be careful that any collections you call Add on are either synchronized inside a lock or are thread safe collections.

For example if SqueareLevenshtein is thread safe internally you could do

Parallel.For(0, strLen, i => { //Are you sure ref is needed here? results[i] = SqueareLevenshtein(ref dimension[i], str.Substring(i, len).ToUpper(), expression, len); }); LevenshteinMatches matches = new LevenshteinMatches(); Parallel.For(0, strLen; i => { if (results[i] <= maxDistance) { lock(matches) { matches.addMatch(str.Substring(i, len), Math.Round((1.0 - ((double)results[i] / len)) * 100.0, 2), i, len, results[i]); } } }); return matches; 

There is nothing async about your function, it is entirely CPU bound work. Changing the signature to async Task<LevenshteinMatches> and never using await in the function should have caused a warning in the compiler.

Instead of "making it async" if the thing you are really after is making it work in parallel then just call the code in parallel.

string s = "xcjavxzcbvmrmummuuutmtumuumtryumtryumtrutryumtryumtrymutryumtyumtryumtrmutyumtrurtmutymurtmyutrymut"; s = string.Concat(Enumerable.Repeat(s, 4000)); var expressions = new[] {"jas", "cbv"} var tasks = new List<Task<LevenshteinMatches>>() foreach(var expression in expressions) { var task = new Task.Run(()=> message.LevenshteinSingleThread(expression, 1)); //Start multiple threads tasks.Add(task); } LevenshteinMatches[] results = Task.WaitAll(tasks); //Wait for all the threads to end. 

You could even make parts of the internal function multi-threaded by using Parallel.For( to make some of the for loops parallel, just be careful that any collections you call Add on are either synchronized inside a lock or are thread safe collections.

There is nothing async about your function, it is entirely CPU bound work. Changing the signature to async Task<LevenshteinMatches> and never using await in the function should have caused a warning in the compiler.

Instead of "making it async" if the thing you are really after is making it work in parallel then just call the code in parallel.

string s = "xcjavxzcbvmrmummuuutmtumuumtryumtryumtrutryumtryumtrymutryumtyumtryumtrmutyumtrurtmutymurtmyutrymut"; s = string.Concat(Enumerable.Repeat(s, 4000)); var expressions = new[] {"jas", "cbv"} var tasks = new List<Task<LevenshteinMatches>>() foreach(var expression in expressions) { var task = new Task.Run(()=> message.LevenshteinSingleThread(expression, 1)); //Start multiple threads tasks.Add(task); } LevenshteinMatches[] results = Task.WaitAll(tasks); //Wait for all the threads to end. 

You could even make parts of the internal function multi-threaded by using Parallel.For( to make some of the for loops parallel, just be careful that any collections you call Add on are either synchronized inside a lock or are thread safe collections.

For example if SqueareLevenshtein is thread safe internally you could do

Parallel.For(0, strLen, i => { //Are you sure ref is needed here? results[i] = SqueareLevenshtein(ref dimension[i], str.Substring(i, len).ToUpper(), expression, len); }); LevenshteinMatches matches = new LevenshteinMatches(); Parallel.For(0, strLen; i => { if (results[i] <= maxDistance) { lock(matches) { matches.addMatch(str.Substring(i, len), Math.Round((1.0 - ((double)results[i] / len)) * 100.0, 2), i, len, results[i]); } } }); return matches; 
Source Link
Scott Chamberlain
  • 128k
  • 37
  • 299
  • 448

There is nothing async about your function, it is entirely CPU bound work. Changing the signature to async Task<LevenshteinMatches> and never using await in the function should have caused a warning in the compiler.

Instead of "making it async" if the thing you are really after is making it work in parallel then just call the code in parallel.

string s = "xcjavxzcbvmrmummuuutmtumuumtryumtryumtrutryumtryumtrymutryumtyumtryumtrmutyumtrurtmutymurtmyutrymut"; s = string.Concat(Enumerable.Repeat(s, 4000)); var expressions = new[] {"jas", "cbv"} var tasks = new List<Task<LevenshteinMatches>>() foreach(var expression in expressions) { var task = new Task.Run(()=> message.LevenshteinSingleThread(expression, 1)); //Start multiple threads tasks.Add(task); } LevenshteinMatches[] results = Task.WaitAll(tasks); //Wait for all the threads to end. 

You could even make parts of the internal function multi-threaded by using Parallel.For( to make some of the for loops parallel, just be careful that any collections you call Add on are either synchronized inside a lock or are thread safe collections.