如何优化比较两个阵列所需的时间?

时间:2014-02-25 05:09:36

标签: c# arrays optimization comparison

我需要一种方法来比较数组,并确定它们的相似程度。我有以下代码:

                        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个元素。有没有办法提高我的比较功能的效率,以便可以多次或至少每秒进行一次比较?谢谢!

2 个答案:

答案 0 :(得分:0)

由于您要将每个元素与其余元素进行比较,因此必然会耗费时间 只有我能想到的优化不是一直计算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次。

答案 1 :(得分:0)

你们各自不依赖于彼此的元素可以使用并行处理来加快它的速度。

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;