【发布时间】:2019-06-27 22:12:22
【问题描述】:
我目前正在解决一个问题,即从另一个名为“ListB”的列表中找到最匹配的数据,即“ListA”。每当我发现“ListA”的元素与“ListB”中的任何元素匹配时,该元素的置信度和准确度为 70% 或更高,我将列表 B 中的匹配字符串和列表 A 中的字符串添加到我进一步的元组中保存在数据库中。
Levenshtien 算法给了我一个数字,我将它与我的阈值 70 进行比较,如果返回的值等于或大于 70% 的阈值,我将其附加到“ListA”的原始字符串元素中。
如果“ListA”和“ListB”中的记录在数千个值范围内,并且如果我将记录增加到一百万个,则我为此过程编写的代码可以正常工作,大约需要一个小时来计算每个值的距离列表 A 的元素。
我需要针对庞大的数据集优化流程。请告知我需要在哪里进行改进。
到目前为止,我的流程代码如下所示
public static PerformFuzzyMatch()
{
// Fetch the ListA & List B from SQL tables
var ListACount = await FuzzyMatchRepo.FetchListACount();
var ListB = await FuzzyMatchRepo.FetchListBAsync();
//Split the ListA data to smaller chunks and loop through those chunks
var splitGroupSize = 1000;
var sourceDataBatchesCount = ListACount / splitGroupSize;
// Loop through the smaller chunks of List A
for (int b = 0; b < sourceDataBatchesCount; b++)
{
var currentBatchMatchedWords = new List<Tuple<string, string, double>>();
int skipRowCount = b * splitGroupSize;
int takeRowCount = splitGroupSize;
// Get chunks of data from ListA according to the skipRowCount and takeRowCount
var currentSourceDataBatch = FuzzyMatchRepository.FetchSourceDataBatch(skipRowCount, takeRowCount);
//Loop through the ListB and parallely calculate the distance between chunks of List A and List B data
for (int i = 0; i < ListB.Count; i++)
{
Parallel.For(
0,
currentSourceDataBatch.Count,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 },
cntr =>
{
try
{
//call the Levenshtien Algorithm to calculate the distance between each element of ListB and the smaller chunk of List A.
int leven = LevenshteinDistance(currentSourceDataBatch[cntr], ListB[i]);
int length = Math.Max(currentSourceDataBatch[cntr].Length, ListB[i].Length);
double similarity = double similarity = 1.0 - (double)leven / length;
if ((similarity * 100) >= 70)
{
currentBatchMatchedWords.Add(Tuple.Create(currentSourceDataBatch[cntr], ListB[i], similarity));
}
cntr++;
}
catch (Exception ex)
{
exceptions.Enqueue(ex);
}
});
}
}
}
它调用的算法是计算距离
public static int LevenshteinDistance(this string input, string comparedTo, bool caseSensitive = false)
{
if (string.IsNullOrWhiteSpace(input) || string.IsNullOrWhiteSpace(comparedTo))
{
return -1;
}
if (!caseSensitive)
{
input = Common.Hashing.InvariantUpperCaseStringExtensions.ToUpperInvariant(input);
comparedTo = Common.Hashing.InvariantUpperCaseStringExtensions.ToUpperInvariant(comparedTo);
}
int inputLen = input.Length;
int comparedToLen = comparedTo.Length;
int[,] matrix = new int[inputLen, comparedToLen];
//initialize
for (var i = 0; i < inputLen; i++)
{
matrix[i, 0] = i;
}
for (var i = 0; i < comparedToLen; i++)
{
matrix[0, i] = i;
}
//analyze
for (var i = 1; i < inputLen; i++)
{
ushort si = input[i - 1];
for (var j = 1; j < comparedToLen; j++)
{
ushort tj = comparedTo[j - 1];
int cost = (si == tj) ? 0 : 1;
int above = matrix[i - 1, j];
int left = matrix[i, j - 1];
int diag = matrix[i - 1, j - 1];
int cell = FindMinimumOptimized(above + 1, left + 1, diag + cost);
//transposition
if (i > 1 && j > 1)
{
int trans = matrix[i - 2, j - 2] + 1;
if (input[i - 2] != comparedTo[j - 1])
{
trans++;
}
if (input[i - 1] != comparedTo[j - 2])
{
trans++;
}
if (cell > trans)
{
cell = trans;
}
}
matrix[i, j] = cell;
}
}
return matrix[inputLen - 1, comparedToLen - 1];
}
寻找最小值优化的实现
public static int FindMinimumOptimized(int a, int b, int c)
{
return Math.Min(a, Math.Min(b, c));
}
【问题讨论】:
-
你永远不应该使用 MaxDegreeOfParallelism = Environment.ProcessorCount * 10。要么你是 CPU 密集型的,你会增加线程开销,要么你是 I/O 密集型的,你应该在你的代码。
-
new List
>();也许你需要一堂课 -
我的意思是要求每个内核运行 10 个线程不会让速度更快。我的意思是多线程不是免费的。看起来您正在使用带有线程的线程不安全集合。
-
List不是线程安全的。你不能像现在这样Add。 -
@Shahid 您需要使用来自
System.Threading.Concurrent命名空间的线程安全集合之一。最接近List<T>的可能是ConcurrentBag<T>
标签: c# performance optimization