I need a way to compare arrays to each other and determine how similar they are. I have the following code:
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;
This code worked fine when I was testing it with small arrays, however when I attempted to implement it in my program, it slowed to a standstill. The ArrayOfArrays contains 45 arrays with ~300,000 elements each. The ArrayToBeCompared is ~300,000 elements as well. Is there any way to increase the efficiency of my comparative function so that a comparison of that size can be done several times or at least once a second? Thanks!