Loop all shapes in query (speed issue)

Hello, i need to speed up a loop trough alot of shapes. Im looping trough them to put all points into a table. This is for calculating the actual position on a road (in meters from the begining of the road).
There can be more than one road on top of each other so i do the loop in a prarallel.foreach.
On one road there is about 5000-10000 loops. This takes about total time of 40sec when there is 3 roads.

             foreach (DataRow row in query)
        {
            Shape = int.Parse(row[14].ToString());
            if (riktning == "Mot")
            {
                if (row.ItemArray[5].ToString() != "Syskonbak")
                {
                    if (row.ItemArray[4].ToString() == "Mot")
                    {
                        for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)
                        {
                            count += 1;
                            MapWinGIS.Point a = new MapWinGIS.Point();
                            MapWinGIS.Point b = new MapWinGIS.Point();
                            a = shapeFile.QuickPoint(Shape, i_shape);
                            b = shapeFile.QuickPoint(Shape, i_shape + 1);
                            shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                            start_a = stop_a;
                            stop_a = start_a + shapeLength;
                            dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                        }
                    }
                    else
                    {
                        for (var i_shape = shapeFile.Shape[Shape].numPoints - 1; i_shape >= 1; i_shape += -1)
                        {
                            count += 1;
                            MapWinGIS.Point a = new MapWinGIS.Point();
                            MapWinGIS.Point b = new MapWinGIS.Point();
                            a = shapeFile.QuickPoint(Shape, i_shape);
                            b = shapeFile.QuickPoint(Shape, i_shape - 1);
                            shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                            start_a = stop_a;
                            stop_a = start_a + shapeLength;
                            dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                        }
                    }
                }
                else
                {
                    for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)
                    {
                        count += 1;
                        MapWinGIS.Point a = new MapWinGIS.Point();
                        MapWinGIS.Point b = new MapWinGIS.Point();
                        a = shapeFile.QuickPoint(Shape, i_shape);
                        b = shapeFile.QuickPoint(Shape, i_shape + 1);
                        shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                        start_a = stop_a;
                        stop_a = start_a + shapeLength;
                        dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                    }
                }
            }
            else
            {
                if (row.ItemArray[4].ToString() == "Mot")
                {
                    for (var i_shape = shapeFile.Shape[Shape].numPoints - 1; i_shape >= 1; i_shape += -1)
                    {
                        count += 1;
                        MapWinGIS.Point a = new MapWinGIS.Point();
                        MapWinGIS.Point b = new MapWinGIS.Point();
                        a = shapeFile.QuickPoint(Shape, i_shape);
                        b = shapeFile.QuickPoint(Shape, i_shape - 1);
                        shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                        start_a = stop_a;
                        stop_a = start_a + shapeLength;
                        dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                    }
                }
                else
                {
                    for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)
                    {
                        count += 1;
                        MapWinGIS.Point a = new MapWinGIS.Point();
                        MapWinGIS.Point b = new MapWinGIS.Point();
                        a = shapeFile.QuickPoint(Shape, i_shape);
                        b = shapeFile.QuickPoint(Shape, i_shape + 1);
                        shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                        start_a = stop_a;
                        stop_a = start_a + shapeLength;
                        dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                    }
                }
            }
        }

Caching the shape you are working on, will help.

So instead of doing:

Shape = int.Parse(row[14].ToString());
for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)

you could do

shapeId = int.Parse(row[14].ToString());
shp =  shapeFile.Shape[shapeId]; 
numPoints = shp.numPoints;
for (var i_shape = 0; i_shape <= numPoints - 2; i_shape++)

You need to avoid to go too many time across the COM-bridge, because that is the most timeconsuming part.

You could also consider getting all points at once using shapeFile.QuickPoints() and then loop through the resulting array.

Thanks for the tips Paul. I’ve managed to speed the loop up so now the time is about 5 000ms instead of 30 000ms.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.