【问题标题】:How can you optimize the time it takes to compare two arrays?如何优化比较两个数组所需的时间?
【发布时间】: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


【解决方案1】:

由于您要将每个元素与其余元素进行比较,因此肯定会很耗时。
我能想到的唯一优化不是一直计算ArrayToBeCompared.Count()。像这样:

int lengthOfArrayToBeCompared = ArrayToBeCompared.Count(); // This step
float totalSame = 0F, percentSame = 0F, totalElements = 0F, sameness;
foreach (var previouslyStoredArray in ArrayOfArrays)
{
    sameness = 0F;
    arrayIndex = 0;
    while (arrayIndex < lengthOfArrayToBeCompared)
    {
        ...

此优化将极大地帮助您。因为你在做ArrayToBeCompared.Count() (45 * 300,000 =) 13,500,000 次。这减少到 1 次。

【讨论】:

  • 为什么不直接使用Array.Length 并节省自己计算元素的时间?
  • @codemonkeh:.Count() 用于枚举器。你说的是数组。
  • @P5Coder 不,.Count() 是一种 Linq 扩展方法,适用于数组支持的所有 IEnumerables。但是.Count() 足够聪明,如果项目实现ICollection,它将在内部使用ICollection.Count,该Array.Length 映射到数组。
【解决方案2】:

您可以使各个元素不相互依赖,可以使用并行处理来加快速度。

float totalSame = 0F, percentSame = 0F, totalElements = 0F;
foreach (var previouslyStoredArray in ArrayOfArrays)
{
    var lockObject = new Object();

    Parallel.For(0, //min index
                 Math.Min(ArrayToBeCompared.Length, previouslyStoredArray.Length), //max index
                 () => 0F, //Initial thread local sameness value
                 (arrayIndex, loopState, localSameness) =>
                 {
                     if (ArrayToBeCompared[arrayIndex] == previouslyStoredArray[arrayIndex])
                         localSameness++;
                     return localSameness;
                 },
                 (localSameness) => 
                 {
                     //This function is not thread safe so we must lock while we aggregate the local counts.
                     lock(lockObject)
                     {
                         totalSame += localSameness;
                     }
                 });
}
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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多