ferday ferday - 10 days ago 5
C# Question

linq count (replace foreach and/or general speed up)

related to a question I asked previously (and got a great answer for)

i have a list roi and an array[][]avg. the list contains an index (of course), each index contains X,Y,Z values in a data structure

i've gotten what i need with a terrible foreach (terribly slow!) and i'm wondering if there is an obvious way to speed this up

for (int i = 0; i < raster_max; i++)
{
foreach (var sublist in RasterSet)
{
foreach (var dataPt in sublist)
{
if (RasterSet.IndexOf(sublist) == i)
{
if (dataPt.Y >= Convert.ToSingle(textBox7.Text) && dataPt.Y <= Convert.ToSingle(textBox6.Text))
{

test[0][i] = dataPt.A - avgbias[6][i];
if (dataPt.A - avgbias[6][i] <= tA)
{
corrA[i]++;
}

}
}
}
}
}


RasterSet is an indexed List of Lists of DataStructures where dataPt is the final datastructure i need to access. raster_max is the length of RasterSet. avgbias[][] is the dataset i generated by using the linked question assistance.

Answer
 foreach (var sublist in RasterSet)
     {
        foreach (var dataPt in sublist)
        {
           if (RasterSet.IndexOf(sublist) == i)
           {
           ...

This looks strange.
Isn't this the same as

   single s7 = Convert.ToSingle(textBox7.Text);
   single s6 = Convert.ToSingle(textBox6.Text);
   for (int i = 0; i < raster_max; i++)
   {
       var sublist = RasterSet[i]

        foreach (var dataPt in sublist)
        {
            if (dataPt.Y >= s7 && dataPt.Y <= s6)
            {
                test[0][i] = dataPt.A - avgbias[6][i];
                if (dataPt.A - avgbias[6][i] <= tA)
                {
                    corrA[i]++;
                }
            }
        }
    }
Comments