Friday, April 5, 2013

Speed test: buffer then cut vs. intersection.buffer then cut

It's a case by case result. If the cutting geometry doesn't have a lot of points (STNumPoints), buffer then cut is faster, as shown below:
                      
         private static void test()
        {
            SqlGeometry poly = SqlGeometry.STGeomFromText(new SqlChars("POLYGON((-4 1, -4 -1, 4 -1, 4 1, -4 1))"), 4269),
                ln1 = SqlGeometry.STGeomFromText(new SqlChars("LINESTRING(-4 0, 4 0)"), 4269),
                ln2 = SqlGeometry.STGeomFromText(new SqlChars("LINESTRING(0 2, 0 -2)"), 4269);
            Stopwatch t1 = new Stopwatch(), t2 = new Stopwatch(), t3 = new Stopwatch(),t4=new Stopwatch();
            for (int i = 0; i < 100; i++)
            {
                t1.Start();
                poly.STDifference(ln2.STBuffer(0.00001));
                t1.Stop();
                t2.Start();
                poly.STDifference(poly.STIntersection(ln2).STBuffer(0.00001));
                t2.Stop();
                t3.Start();
                ln1.STDifference(ln2.STBuffer(0.00001));
                t3.Stop();
                t4.Start();
                ln1.STDifference(ln1.STIntersection(ln2).STBuffer(0.00001));
                t4.Stop();
            }
            Debug.WriteLine("t1=" + TimeSpan.FromMilliseconds(t1.ElapsedMilliseconds));
            Debug.WriteLine("t2=" + TimeSpan.FromMilliseconds(t2.ElapsedMilliseconds));
            Debug.WriteLine("t3=" + TimeSpan.FromMilliseconds(t3.ElapsedMilliseconds));
            Debug.WriteLine("t4=" + TimeSpan.FromMilliseconds(t4.ElapsedMilliseconds));
        }
[result]
t1=00:00:00.0280000
t2=00:00:00.0310000
t3=00:00:00.0250000
t4=00:00:00.0980000

If the cutting geometry has a lot of points, intersection.buffer then cut is faster, as shown below:
        private static void test()
        {
            StringBuilder sb = new StringBuilder("LINESTRING(");            
            for (int i = -1000; i < 1000; i++)
            {
                sb.Append(i + " 0");
                if (i != 999)
                    sb.Append(",");
                else
                    sb.Append(")");
            }

            SqlGeometry poly = SqlGeometry.STGeomFromText(new SqlChars("POLYGON((-4 1, -4 -1, 4 -1, 4 1, -4 1))"), 4269),
                ln1 = SqlGeometry.STGeomFromText(new SqlChars("LINESTRING(-4 0, 4 0)"), 4269),
                ln2 = SqlGeometry.STGeomFromText(new SqlChars(sb.ToString()), 4269);
            Stopwatch t1 = new Stopwatch(), t2 = new Stopwatch(), t3 = new Stopwatch(),t4=new Stopwatch();
            for (int i = 0; i < 100; i++)
            {
                t1.Start();
                poly.STDifference(ln2.STBuffer(0.00001));
                t1.Stop();
                t2.Start();
                poly.STDifference(poly.STIntersection(ln2).STBuffer(0.00001));
                t2.Stop();
                t3.Start();
                ln1.STDifference(ln2.STBuffer(0.00001));
                t3.Stop();
                t4.Start();
                ln1.STDifference(ln1.STIntersection(ln2).STBuffer(0.00001));
                t4.Stop();
            }
            Debug.WriteLine("t1=" + TimeSpan.FromMilliseconds(t1.ElapsedMilliseconds));
            Debug.WriteLine("t2=" + TimeSpan.FromMilliseconds(t2.ElapsedMilliseconds));
            Debug.WriteLine("t3=" + TimeSpan.FromMilliseconds(t3.ElapsedMilliseconds));
            Debug.WriteLine("t4=" + TimeSpan.FromMilliseconds(t4.ElapsedMilliseconds));
        }
[result]
t1=00:00:01.3610000
t2=00:00:00.0770000
t3=00:00:01.3190000
t4=00:00:00.0510000

No comments:

Post a Comment