I made a simple array that holds 2,000,000 ints for holding all the RGBs and a second array that holds 2,000,000 ints for the amount of times that rgb has been detected. Then I have it loop through all 6,000,000 bytes of a picture like so:
uint[] colors = new uint[rawImageBytes.Length / 3];
int[] hits = new int[rawImageBytes.Length / 3];
int colorAmount = 0;
int totalRepeats = 0;
int lastTime = Environment.TickCount;
int lastCount = 0;
uint currentColor = 0;
bool found;
for (int i = 0; i < rawImageBytes.Length - 3; i += 3)
{
if (Environment.TickCount - lastTime > 10000)
{
setStatus(((i - lastCount)/10) + " checks per second");
lastTime = Environment.TickCount;
lastCount = i;
}
currentColor = (uint)((rawImageBytes[i] << 0) | (rawImageBytes[i + 1] << 8) | (rawImageBytes[i + 2] << 16));
//set it to false to see if pattern exists
found = false;
//check all patterns
for (int k = 0; k < colorAmount; k++)
{
//if pattern exists
if (colors[k] == currentColor)
{
//dont add it and increase the hit instead
found = true;
hits[k]++;
}
}
//if pattern does not exist, set it
if (found == false)
{
colors[colorAmount] = currentColor;
hits[colorAmount] = 0;
colorAmount++;
}
}
And my log shows that they speed slows down significantly from the search range increasing
5724 checks per second
5847 checks per second
5959 checks per second
6044 checks per second
6318 checks per second
7096 checks per second
8530 checks per second
10680 checks per second
16233 checks per second
11469 checks per second
How can I make my search more efficient so that it doesn’t take 20 minutes?
First issue I see is that your hits array is ecxessively large. If you assume that one color may be hit more than once, then your hits array must me shorter than your colors array.
Second issue I see is that you don’t stop iterating after you found your color in colors array. You should place break; after found = true; statement.
Why you don’t like Dictionary< uint,int> type for your hits collection? Your color should be a Key and hits count should be a value:
And also try enabling optimization instruction for compiler.