【发布时间】:2014-02-25 05:09:35
【问题描述】:
我需要一种方法来相互比较数组并确定它们的相似程度。我有以下代码:
float totalSame = 0F, percentSame = 0F, totalElements = 0F, sameness;
foreach (var previouslyStoredArray in ArrayOfArrays)
{
sameness = 0F;
arrayIndex = 0;
while (arrayIndex < ArrayToBeCompared.Count())
{
/*Compares an element from ArrayToBeCompared with the corresponding
position in all past arrays stored in ArrayOfArrays. When they are the same, the variable 'sameness'
is an increased by a value of 1. Sameness represents the number of same
elements within a single, previously stored array and the ArrayToBeCompared. 'totalSame' represents
the total number of elements that are the same between the ArrayToBeCompared and all arrays in the ArrayOfArrays.*/
if (ArrayToBeCompared[arrayIndex] == previouslyStoredArray[arrayIndex])
{
sameness++;
}
arrayIndex++;
}
totalSame = sameness + totalSame;
}
totalElements = ArrayToBeCompared.Length * ArrayOfArrays.Length;
//By taking the total number of similar elements and dividing by the total
//number of elements we can get the percentage that are similar
percentSame = totalSame / totalElements * 100F;
当我用小数组测试它时,这段代码运行良好,但是当我试图在我的程序中实现它时,它变慢了。 ArrayOfArrays 包含 45 个数组,每个数组约 300,000 个元素。 ArrayToBeCompared 也是 ~300,000 个元素。有什么方法可以提高我的比较函数的效率,以便可以多次或至少每秒进行一次该大小的比较?谢谢!
【问题讨论】:
-
Comparing Arrays in C# 的可能重复项
-
您正在比较每个项目,并根据它们的相关程度得出一个值。这需要检查一切。大多数比较都会使整个事情短路,最终不会产生价值。
-
你能更正显示代码的缩进吗?
标签: c# arrays optimization comparison