I have this piece of code that is running too slow. I was wondering if anyone can help me optimize it as I can't seem to find any more shortcuts. I'm not sure if using List<>
is going to help me but I need complex operation such as Union and Overlap.
Also, List
is desirable because I don't know how many unique partitions an Image Region is going to be before running.
u1.length
= 13254 which is the number of distinct elements in RevisedListMeanH
RevisedListMeanH.Count
= 90000
The purpose of the first piece of code is to group together pixels via horizontal comparison and vertical comparison. This runs for about 70 seconds.
The second portion combines both vertical and horizontal pixel blocks into a 2D-block. This section runs for about 120 seconds. My goal is to have both of these loops complete under 10 seconds.
These numbers are from a 300x300 pixel region comparisons of a 4000x3000 image.
watch.Start();
for (int s = 0; s < u1.Length; s++ )//iterate through uniquelist
{
List<int> ConnectedBlocksH = new List<int>();
List<int> ConnectedBlocksV = new List<int>();
float[] RH = RevisedListMeanH.ToArray();
float[] RV = RevisedListMeanV.ToArray();
for (int a = 0; a < RevisedListMeanH.Count; a++)//iterate through bitmap with res
{
if (u1[s] == RH[a])
{
ConnectedBlocksH.Add(a);//add the index
}
}
for (int a = 0; a < RevisedListMeanV.Count; a++)//iterate through bitmap with res
{
if (u1[s] == RV[a])
{
ConnectedBlocksV.Add(a);//add the index
}
}
ArrayOfConnectedBlocksH[s] = ConnectedBlocksH;//where the data IS
ArrayOfConnectedBlocksV[s] = ConnectedBlocksV;//where the data IS
}
watch.Stop();
long asasasdda = watch.ElapsedMilliseconds; //71 sec
watch.Reset();
watch.Start();
//if intersects then union the two lists
//iterate through both list
List<List<int>> ListOfConnectedBlocks = new List<List<int>>();
for (int a = 0; a < ArrayOfConnectedBlocksH.Length; a++ )
{
HashSet<int> i = new HashSet<int>(ArrayOfConnectedBlocksH[a]);
//trigger means it scanned and there was no overlap to add to the group
while (true)
{
bool trigger = true;
for (int b = 0; b < ArrayOfConnectedBlocksV.Length; b++)
{
if (i.Overlaps(ArrayOfConnectedBlocksV[b]))
{
i.UnionWith(ArrayOfConnectedBlocksV[b]);//combines all overlaps into one group
ArrayOfConnectedBlocksV[b].Clear();//merged so just remove
trigger = false;
}
}
if (trigger)
{
break;
}
trigger = true;
for (int c = 0; c < ArrayOfConnectedBlocksH.Length; c++)//now cycle through horizontal
{
if (i.Overlaps(ArrayOfConnectedBlocksH[c]))
{
i.UnionWith(ArrayOfConnectedBlocksH[c]);//combines all overlaps into one group
ArrayOfConnectedBlocksH[c].Clear();//merged so just remove
trigger = false;
}
}//first cycle to get T0
if(trigger)
{
break;
}
}
if (i.Count != 0)
{
ListOfConnectedBlocks.Add(i.ToList<int>());
}
}
watch.Stop();
long asasasda = watch.ElapsedMilliseconds;//122 seconds
List.Add()
in a loop inside anif
, because to me that means its just begging for an.AddRange()
and a.Where()
, but the fact that you're adding the index makes that far too difficult. Still, creating two arrays each iteration and growing them one by one in the inner loops is certainly worth looking carefully at when trying to determine what's slowing you down. \$\endgroup\$