我有计算Levenshtein距离的方法
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();
}
}
我该怎么做才能让它异步?
或者我应该留下这种方法,只是以不同的方式调用它?
这是我试图让它异步;不知道出了什么问题,但我无法得到任何结果 - 线程无法工作,但它只需要几毫秒。
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();
}
});
}
其余代码link
这就是我的称呼方式:
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;
答案 0 :(得分:5)
您的功能没有任何异步,它完全是CPU绑定的工作。将签名更改为async Task<LevenshteinMatches>
并且从不在函数中使用await
应该在编译器中引发警告。
如果您真正追求的是并行工作,那么只需并行调用代码,而不是“使其异步”。
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.
你甚至可以通过使用Parallel.For(
使一些for循环并行来使内部函数的一部分多线程化,只要注意你调用Add
的任何集合都要在一个内部同步lock
或者是线程安全的集合。
例如,如果SqueareLevenshtein
内部是线程安全的,则可以执行
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;